python 验证码

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

str

def rdChar():
return chr(random.randint(60,90))

def rdColor():
return (random.randint(200,255),random.randint(100,200),random.randint(10,50))

def rdColor1():
return (random.randint(32, 127), random.randint(32, 127), random.randint(32, 127))

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

创建Font对象:

font = ImageFont.truetype('Arial.ttf', 36)

创建Draw对象:

draw = ImageDraw.Draw(image)

填充每个像素:

for x in range(width):
for y in range(height):
draw.point((x, y), fill=rdColor())

输出文字:

for t in range(5):
draw.text((60 * t + 10, 10), rdChar(), font=font, fill=rdColor1())

模糊:

image = image.filter(ImageFilter.BLUR)
image.save('demo.jpg', 'jpeg');

posted @ 2016-01-14 13:26  gopher-lin  阅读(212)  评论(0编辑  收藏  举报