使用python生成验证码

生成一个包含四个字母的验证码~

#!/usr/bin/env python
# -*- coding: utf-8 -*-

from PIL import Image, ImageDraw, ImageFont, ImageFilter
import random

# random character
def rndChar():
    return chr(random.randint(65, 90))

# random color 1
def rndColor1():
    return (random.randint(64, 255), random.randint(64, 255), random.randint(64, 255))

# random color 2
def rndColor2():
    return (random.randint(32, 127), random.randint(32, 127), random.randint(32, 127))

# 240 * 60
width = 60 * 4
height = 60
image = Image.new('RGB', (width, height), (255, 255, 255))

# create Font object
myfont = ImageFont.truetype('C:/Windows/Fonts/Calibri.ttf', 36)

# create Draw object
draw = ImageDraw.Draw(image)

# fill in every pixel
for x in range(width):
    for y in range(height):
        draw.point((x, y), fill = rndColor1())

# output the text
for t in range(4):
    draw.text((60 * t + 10, 10), rndChar(), font = myfont, fill = rndColor2())

# fuzzy
image = image.filter(ImageFilter.BLUR)
image.save('newImg.jpg','jpeg')

效果如下:



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值