python绘制验证码
import random
from PIL import Image, ImageDraw, ImageFont
if __name__ == '__main__':
image = Image.new(mode='RGB', size=(200, 40), color=(200, 220, 210))
draw_brush = ImageDraw.Draw(image, mode='RGB')
character_font = ImageFont.truetype('arial.ttf', 28)
for i in range(7):
character = random.choice([chr(random.randint(65, 95)), str(random.randint(0, 9))])
character_color = (random.randint(0, 255),random.randint(0, 255), random.randint(0, 255))
draw_brush.text([i*30, 0], character, character_color, font=character_font)
draw_brush.line((random.randint(0, 10), random.randint(0, 55), random.randint(10,1000), random.randint(0, 45)), 'cyan')
with open("../data/yanzhengma.png", 'wb') as fp:
image.save(fp, format="png")
参数
图片来源:爱犬小仙猪Python PIL之ImageDraw类详解