import random
from PIL import Image, ImageDraw, ImageFont
width, height, font_size, font_num = 300, 100, 48, 4
bg_color = (random.randint(0, 255), random.randint(0, 255), random.randint(0, 255))
image = Image.new(mode='RGB', size=(width, height), color=bg_color)
draw = ImageDraw.Draw(image, mode='RGB')
font = ImageFont.truetype("C:\\Windows\\Fonts\\Arial.ttf", font_size)
verify = str()
for i in range(font_num):
x = random.randint(i*(width/font_num), (i+1)*(width/font_num)-font_size)
y = random.randint(0, height-font_size)
char = random.choice([chr(alpha) for alpha in range(65, 117)] + [str(num) for num in range(10)])
color = (random.randint(0, 255), random.randint(0, 255), random.randint(0, 255))
draw.text((x, y), char, fill=color, font=font)
image.show()
Python 产生随机验证码
最新推荐文章于 2024-04-18 09:32:45 发布