用 python 写验证码的例子

来源:互联网 发布:no热力学数据 编辑:程序博客网 时间:2024/05/01 07:13
import Image, ImageDraw, ImageFont, ImageFilterimport random#letterdef rndChar():    return chr(random.randint(65, 90))# color1def rndColor():    return (random.randint(64, 255), random.randint(64, 255), random.randint(64, 255))# color2:def rndColor2():    return (random.randint(32, 127), random.randint(32, 127), random.randint(32, 127))# 240 x 60:width = 60 * 4height = 60image = Image.new('RGB', (width, height), (255, 255, 255))# make Font object:font = ImageFont.truetype('Arial.ttf', 36)# make Draw object:draw = ImageDraw.Draw(image)# fill pixelsfor x in range(width):    for y in range(height):        draw.point((x, y), fill=rndColor())# print letterfor t in range(4):#the show letter is 4    draw.text((60 * t + 10, 10), rndChar(), font=font, fill=rndColor2())# blurimage = image.filter(ImageFilter.BLUR)image.save('code.jpg', 'jpeg');image.show()
#后添加汉子会出现报错如下:
but no encoding declared see http //python.org/dev/peps/pep-0263/ for details
解决方案:把#后面的数字变为英文就可以了。
for t in range(4):#the show letter is 4    draw.text((60 * t + 10, 10), rndChar(), font=font, fill=rndColor2())
这个一部分是字体的显示,提供了一个循环显示的例子。
                                             
0 0
原创粉丝点击