Python Imaging Library Handbook
http://effbot.org/imagingbook/pil-index.htm
随机验证码的产生
1 import Image, ImageDraw, ImageFont, ImageFilter 2 import random 3 4 # 随机字母: 5 def rndChar(): 6 return chr(random.randint(65, 90)) 7 8 # 随机颜色1: 9 def rndColor(): 10 return (random.randint(64, 255), random.randint(64, 255), random.randint(64, 255)) 11 12 # 随机颜色2: 13 def rndColor2(): 14 return (random.randint(32, 127), random.randint(32, 127), random.randint(32, 127)) 15 16 # 240 x 60: 17 width = 60 * 4 18 height = 60 19 image = Image.new(‘RGB‘, (width, height), (255, 255, 255)) 20 # 创建Font对象: 21 font = ImageFont.truetype(‘Arial.ttf‘, 36) 22 # 创建Draw对象: 23 draw = ImageDraw.Draw(image) 24 # 填充每个像素: 25 for x in range(width): 26 for y in range(height): 27 draw.point((x, y), fill=rndColor()) 28 # 输出文字: 29 for t in range(4): 30 draw.text((60 * t + 10, 10), rndChar(), font=font, fill=rndColor2()) 31 # 模糊: 32 image = image.filter(ImageFilter.BLUR) 33 image.save(‘code.jpg‘, ‘jpeg‘);
时间: 2024-11-05 22:32:59