problem0010生成字母验证码
第 0010 题: 使用 Python 生成类似于下图中的字母验证码图片
- random模块产生随机字母与背景和字体填充颜色
- PIL模块画图
demo:
#!/bin/python3
from PIL import Image,ImageDraw,ImageFilter,ImageFont
import random
def randomChar(): # return English letter
if random.randint(0,1)==0 :
return(chr(random.randint(65,90)))
else:
return chr(random.randint(97,122))
def randomBlackgroundColor():
return (random.randint(64,255),random.randint(64,255),random.randint(64,255))
def randomWordColor():
return (random.randint(32,127),random.randint(32,127),random.randint(32,127))
def createImage():
im = Image.new('RGB',(240,60),(255,255,255))
font = ImageFont.truetype('./simhei.ttf',40)
draw = ImageDraw.Draw(im)
for x in range(240):
for y in range(60):
draw.point((x,y),randomBlackgroundColor())
words =' '
for i in range(4):
word = randomChar()
draw.text((50*i+random.randint(10,40),random.randint(0,20)),word,font=font,fill=randomWordColor())
words += word
img = im.filter(ImageFilter.BLUR)
im.save('result.png')
print(words)
if __name__=='__main__':
createImage()
参考:python练习册之10