tornado的验证码显示 由于本博客供自己复习使用 因此十分粗糙
首先 安装pillow包 pip install pillow
画图
from PIL import Image, ImageDraw, ImageFont
def create_img_code():
code_list = []
width = 240
height = 80
# 生成画板
image = Image.new('RGB', (width, height), (255, 255, 255))
# 生成字体
font = ImageFont.truetype('static/fonts/ADOBEARABIC-BOLD.OTF', 60)
# 生成画笔
draw = ImageDraw.Draw(image)
# 生成 随机 4 位 验证码
def rand_char():
char_source = "qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM1234567890"
code = random.choice(char_source)
code_list.append(code)
return code
# 随机颜色
def rand_color():
return (random.randint(0, 255), random.randint(0, 255), random.randint(0, 255))
# 在画板上画出验证码
for i in range(4):
draw.text((60 * i + 10, 10), rand_char(), font=font, fill=rand_color())
# 加入 5000 噪点
for i in range(5000):