# encoding: utf-8
import sys,getopt
from captcha.image import ImageCaptcha
from random import randint
import base64
from io import BytesIO
# 此类用于制作验证码
class Captcha():
@classmethod
def get_captcha(cls):
list = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't',
'u', 'v', 'w', 'x', 'y', 'z',
'', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T',
'U', 'V', 'W', 'X', 'Y', 'Z']
chars = ''
for i in range(4):
chars += list[randint(0, 62)]
image = ImageCaptcha().generate_image(chars)
buffered = BytesIO()
image.save(buffered, format="PNG")
img = b"data:image/png;base64," + base64.b64encode(buffered.getvalue())
return chars,img
if __name__ == "__main__":
# 运行,得到返回text和image
Captcha.get_captcha()
python生成验证码并返回base64
最新推荐文章于 2024-02-07 11:58:15 发布