Python生成验证码
from PIL import Image,ImageDraw,ImageFont,ImageFilter
import random
def rndNum():#产生随机数字
return random.randint(10,100)
def rndBGcolor():#产生背景每个像素点的随机数
return (random.randint(125,255),random.randint(125,255),random.randint(125,255))
def rndFcolor():#产生每个数字的随机色
return (random.randint(0,130),random.randint(0,130),random.randint(0,130))
height=60
width=4*height
image=Image.new('RGB',(width,height),rndBGcolor()) #生成图片背景
#1.填充背景
draw=ImageDraw.Draw(image)
for x in range(width):
for y in range(height):
draw.point((x,y),fill=rndBGcolor())
#2.填充数字
font=ImageFont.truetype("GIGI.TTF",36)
for i in range(4):
draw.text((60*i+10,10),str(rndNum()),font=font, fill=rndFcolor())
#3.加个模糊滤镜
image=image.filter(ImageFilter.BLUR)
image.save('code.jpg','jpeg')#保存
image.show()#查看结果