绘制验证码

 1 from PIL import Image,ImageFont,ImageDraw
 2 import random
 3 width,height = 100,100
 4 img = Image.new('RGB',(width,height),(255,255,255))
 5 # 获取draw_obj对象
 6 draw_obj =  ImageDraw.Draw(img)
 7 def get_color():
 8     return(random.randint(200,255),random.randint(200,255),random.randint(200,255))
 9 for x in range(width):
10     for y in range(height):
11         draw_obj.point((x,y),fill=get_color())
12 # 生成随机字母
13 def get_char():
14     return chr(random.randint(65,97))
15 font = ImageFont.truetype('simsun.ttc',36)
16 # 绘制随机字母
17 for i in range(4):
18     draw_obj.text((10+i*20,50),get_char(),font=font,fill=(255,0,0))
19 # 绘制干扰线
20 draw_obj.line((10,10,80,80),fill=(0,255,0),width=5)
21 img.show()

 

posted @ 2020-05-20 09:53  小他_W  阅读(162)  评论(0编辑  收藏  举报