def generate_verify_image(font_path):
"""
???????
:param font_path: ???????????
:return: ????????base64?????
"""
width = 60 * 4
height = 60
image = Image.new('RGB', (width, height), (255, 255, 255))
# ??Font??:
font = ImageFont.truetype(font_path, 36)
# ??Draw??:
draw = ImageDraw.Draw(image)
# ??????:
for x in range(width):
for y in range(height):
draw.point((x, y), fill=rand_background_color())
# ????:
rand_str = rand_char()
rand_str += rand_char()
rand_str += rand_char()
rand_str += rand_char()
for t in range(4):
draw.text((60 * t + 10, 10), rand_str[t], font=font, fill=rand_text_color())
# ??:
image = image.filter(ImageFilter.BLUR)
file_name = './static/img/' + str(time.time())
file_name += '.jpg'
image.save(file_name, 'jpeg')
f = open(file_name, 'rb')
str_image = b'data:image/jpeg;base64,'
str_image += base64.b64encode(f.read())
f.close()
os.remove(file_name)
return rand_str, bytes.decode(str_image)
评论列表
文章目录