效果:
image.png
from PIL import Image, ImageFont, ImageDraw, ImageFilter
import random
# 绘制验证码
img = Image.new('RGB', (200, 100), color='White')
draw_obj = ImageDraw.Draw(img)
def get_color():
#生成随机颜色
return random.randint(0, 255), random.randint(0, 255), random.randint(0, 255)
def get_char():
# 生成随机数字或字母的组合
import string
return random.choice(string.ascii_letters+string.digits)
for i in range(200):
for j in range(100):
draw_obj.point((i, j), fill=get_color())
font = ImageFont.truetype('C:\\Windows\\Fonts\\phagspab.ttf', 60)
for i in range(4):
draw_obj.text((i * 50, random.randint(0,50)), text=get_char(), font=font, fill=random.choice(['red','blue','yellow','black']))
# 加干扰线-随机加50条干扰线
for i in range(50):
draw_obj.line((random.randint(0,200),random.randint(0,100),random.randint(0,200),random.randint(0,100)),fill='black')
img.filter(ImageFilter.EMBOSS)
img.show()
网友评论