- 基于第三方库
Pillow
生成验证码图片 - 注意: 若不指定字体类型,需要把文件中的
msyh.ttf
拷贝到系统字体库中
draw = ImageDraw.Draw(img) # 创建画笔
def create_lines():
"""绘制干扰线
"""
line_num = random.randint(*n_line) # 干扰线条数
for i in range(line_num):
# 起始点
begin = (random.randint(0, size[0]), random.randint(0, size[1]))
# 结束点
end = (random.randint(0, size[0]), random.randint(0, size[1]))
draw.line([begin, end], fill=(0, 0, 0))
def create_points():
"""绘制干扰点
"""
chance = min(100, max(0, int(point_chance))) # 大小限制在[0, 100]
for w in range(width):
for h in range(height):
tmp = random.randint(0, 100)
if tmp > 100 - chance:
draw.point((w, h), fill=(0, 0, 0))
def create_text(xy_, font_type_):
"""绘制验证码字符
"""
c_chars = get_chars()
text_ = '%s' % ' '.join(c_chars) # 每个字符前后以空格隔开
if font_type_ is None:
curr_path = os.path.dirname(os.path.abspath(__file__))
font_type_ = os.path.join(os.path.dirname(curr_path), "packages", "msyh.ttf")
font = ImageFont.truetype(font_type_, font_size)
if xy_ is None:
font_width, font_height = font.getsize(text_)
xy_ = ((width - font_width) / 3, (height - font_height) / 3) # 左右距离/上下距离
draw.text(xy_, text_, font=font, fill=fg_color)
return ''.join(c_chars)
# 图片路径
file_name_ = file_name if file_name else text
file_name = "{}.{}".format(file_name_, suffix)
file_path = os.path.join(image_path, file_name)
img.save(file_path)