生成验证码
from PIL import Image,ImageDraw,ImageFont,ImageFilter
import random
#随机字母
def rndChar():
return chr(random.randint(65,90))
#背景随机颜色
def rndColor():
return (random.randint(64,255),random.randint(64,255),random.randint(64,255))
#字体随机颜色2
def rndColor2():
return (random.randint(32,127),random.randint(32,127),random.randint(32,127))
width = 60*4
height = 60
#创建240*60的白底image图象
image = Image.new('RGB',(width,height),(255,255,255))
#字体类型与大小
font = ImageFont.truetype('arial.ttf',48)
#为对象提供简单的2D图形
draw = ImageDraw.Draw(image)
#通过循环每一个像素点填充背景颜色
for x in range(width):
for y in range (height):
draw.point((x,y),fill=rndColor())
for t in range(4):
draw.text((60*t+10,10),rndChar(),font = font,fill=rndColor2())
#对图像进行模糊过滤
image = image.filter(ImageFilter.BLUR)
image.save('code.jpg','jpeg')