04 第三方之验证码封装

  安装模块

1 pip install flask
2 pip install pillow

  代码块

  1 from flask import Flask,make_response
  2 import random
  3 import string
  4 from io import BytesIO
  5 app = Flask(__name__)
  6 
  7 from PIL import Image, ImageDraw, ImageFont
  8 
  9 
 10 class Captcha:
 11    # 生成的验证码的个数
 12    number = 4
 13    # 图片的宽度和高度
 14    size = (80, 30)
 15    # 字体大小
 16    fontsize = 25
 17    # 干扰线条数
 18    line_number = 2
 19 
 20    SOURCE = list(string.ascii_letters)
 21    SOURCE.extend(map(str, list(range(0, 10))))
 22 
 23    @classmethod
 24    def __gen_line(cls, draw, width, height):
 25       """
 26       绘制干扰线
 27       """
 28       begin = (random.randint(0, width), random.randint(0, height))
 29       end = (random.randint(0, width), random.randint(0, height))
 30       draw.line([begin, end], fill=cls.__gen_random_color(), width=2)
 31 
 32    @classmethod
 33    def __gen_random_color(cls, start=0, end=255):
 34       """
 35       产生随机颜色
 36       颜色的取值范围是0~255
 37       """
 38       random.seed()
 39       return (
 40          random.randint(start, end),
 41          random.randint(start, end),
 42          random.randint(start, end),
 43       )
 44 
 45    @classmethod
 46    def __gen_points(cls, draw, point_chance, width, height):
 47       """
 48       绘制干扰点
 49       """
 50       chance = min(100, max(0, int(point_chance)))
 51       for w in range(width):
 52          for h in range(height):
 53             temp = random.randint(0, 100)
 54             if temp > 100 - chance:
 55                draw.point((w, h), fill=cls.__gen_random_color())
 56 
 57    @classmethod
 58    def __gen_random_font(cls):
 59       """
 60       采用随机字体
 61       :return:
 62       """
 63       fonts = ["consola.ttf", "consolab.ttf", "consolai.ttf"]
 64       font = random.choice(fonts)
 65       return "utils/captcha/" + font
 66 
 67    @classmethod
 68    def gen_text(cls, number):
 69       """
 70       随机生成一个字符串
 71       :param number: 字符串数量
 72       """
 73       return "".join(random.sample(cls.SOURCE, number))
 74 
 75    @classmethod
 76    def gen_graph_captcha(cls):
 77       width, height = cls.size
 78       # A表示透明度
 79       image = Image.new("RGBA", (width, height), cls.__gen_random_color(0, 100))
 80       # 字体
 81       font = ImageFont.truetype(cls.__gen_random_font(), cls.fontsize)
 82       # 创建画笔
 83       draw = ImageDraw.Draw(image)
 84       # 生成随机字符串
 85       text = cls.gen_text(cls.number)
 86       # 字体大小
 87       font_width, font_height = font.getsize(text)
 88       # 填充字符串
 89       draw.text(
 90          ((width - font_width) / 2, (height - font_height) / 2),
 91          text,
 92          font=font,
 93          fill=cls.__gen_random_color(150, 255),
 94       )
 95       # 绘制干扰线
 96       for x in range(0, cls.line_number):
 97          cls.__gen_line(draw, width, height)
 98       # 绘制噪点
 99       cls.__gen_points(draw, 10, width, height)
100 
101       return text, image
102 
103 
104 @app.route('/captcha/')
105 def graph_captcha():
106    text, image = Captcha.gen_graph_captcha()
107    out = BytesIO()
108    image.save(out, 'png')
109    out.seek(0)
110    resp = make_response(out.read())
111    resp.content_type = 'image/png'
112    return resp
113 
114 
115 if __name__ == '__main__':
116     app.run()

  验证码图样

       

猜你喜欢

转载自www.cnblogs.com/a2534786642/p/11040699.html