生成字母验证码图片

生成图片样例:

# coding=utf-8
from PIL import Image,ImageDraw,ImageFont,ImageFilter
import random
import string

def rndColor():
    return (random.randint(0, 255), random.randint(0, 255), random.randint(0, 255))
width = 240
height = 60
im = Image.new('RGB', (240, 60), (255, 255, 255))
dr = ImageDraw.Draw(im)

for x in range(width):
    for y in range(height):
        dr.point((x, y), fill=rndColor())

font = ImageFont.truetype('C:/Windows/Fonts/Arial.ttf', 40)

for i in range(4):
    dr.text((i*60+10, 10), random.choice(string.letters), font=font, fill="#000000")

im = im.filter(ImageFilter.BLUR)
im.show()
im.save('E:\debug\python\yzCode\l.jpg', 'jpeg')


你可能感兴趣的:(Python,验证码,图片)