想生成字母是倾斜的验证码,代码如下:
#!/usr/bin/env python3 #-*- coding:utf-8 -*- #生成验证码图片 from PIL import Image,ImageDraw,ImageFont,ImageFilter import random #chr 转成相应的 ascll 值 def rndChar(): return chr(random.randint(65,90)) def rndColor(): return (random.randint(64,255),random.randint(64,255),random.randint(64,255)) def rndColor2(): 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= ImageFont.truetype('Arial.ttf',36) draw = ImageDraw.Draw(image) for x in range(width): for y in range(height): draw.point((x,y),fill=rndColor()) word='' for t in range(4): imaget = Image.new('RGB',(60,60),(255,255,255)) drawt = ImageDraw.Draw(imaget) for x in range(60): for y in range(60): drawt.point((x,y),fill=rndColor()) c = rndChar() word=word+c drawt.text((12,12),c,fOnt=font,fill=rndColor2()) imaget = imaget.rotate(random.randint(-30,30)) image.paste(imaget,(60*t,0)) # for t in range(4): # draw.text((60*t+10,10),rndChar(),fOnt=font,fill=rndColor2()) image = image.filter(ImageFilter.BLUR) image.save('hehe.jpg','jpeg') print('word is %s ' % (word)) 主要是想其中的文字能够倾斜。。。
结果生成这样了:
生成的图片
怎么才能去除那些嘿嘿的东西呢。。。
