Python code for generating image verification code

From , 4 Years ago, written in Python, viewed 55 times.
URL https://pastebin.vip/view/bdc36378
  1. #!/usr/bin/env python  
  2. #coding=utf-8  
  3.  
  4. import random  
  5. import Image, ImageDraw, ImageFont, ImageFilter  
  6. import StringIO
  7.  
  8. from flask import Flask
  9.  
  10. #map:将str函数作用于后面序列的每一个元素
  11. numbers = ''.join(map(str, range(10)))
  12. chars = ''.join((numbers))  
  13.  
  14. def create_validate_code(size=(120, 30),  
  15.                          chars=chars,  
  16.                          mode="RGB",  
  17.                          bg_color=(255, 255, 255),  
  18.                          fg_color=(255, 0, 0),  
  19.                          font_size=18,  
  20.                          font_type="Monaco.ttf",  
  21.                          length=4,  
  22.                          draw_points=True,  
  23.                          point_chance = 2):  
  24.     '''''
  25.    size: 图片的大小,格式(宽,高),默认为(120, 30)
  26.    chars: 允许的字符集合,格式字符串
  27.    mode: 图片模式,默认为RGB
  28.    bg_color: 背景颜色,默认为白色
  29.    fg_color: 前景色,验证码字符颜色
  30.    font_size: 验证码字体大小
  31.    font_type: 验证码字体,默认为 Monaco.ttf
  32.    length: 验证码字符个数
  33.    draw_points: 是否画干扰点
  34.    point_chance: 干扰点出现的概率,大小范围[0, 50]
  35.    '''
  36.  
  37.     width, height = size  
  38.     img = Image.new(mode, size, bg_color) # 创建图形  
  39.     draw = ImageDraw.Draw(img) # 创建画笔  
  40.  
  41.     def get_chars():  
  42.         '''''生成给定长度的字符串,返回列表格式'''
  43.         return random.sample(chars, length)  
  44.  
  45.     def create_points():  
  46.         '''''绘制干扰点'''
  47.         chance = min(50, max(0, int(point_chance))) # 大小限制在[0, 50]  
  48.  
  49.         for w in xrange(width):  
  50.             for h in xrange(height):  
  51.                 tmp = random.randint(0, 50)  
  52.                 if tmp > 50 - chance:  
  53.                     draw.point((w, h), fill=(0, 0, 0))  
  54.  
  55.     def create_strs():  
  56.         '''''绘制验证码字符'''
  57.         c_chars = get_chars()  
  58.         strs = '%s' % ''.join(c_chars)  
  59.  
  60.         font = ImageFont.truetype(font_type, font_size)  
  61.         font_width, font_height = font.getsize(strs)  
  62.  
  63.         draw.text(((width - font_width) / 3, (height - font_height) / 4),  
  64.                     strs, font=font, fill=fg_color)  
  65.  
  66.         return strs  
  67.  
  68.     if draw_points:  
  69.         create_points()  
  70.     strs = create_strs()  
  71.  
  72.     # 图形扭曲参数  
  73.     params = [1 - float(random.randint(1, 2)) / 100,  
  74.               0,  
  75.               0,  
  76.               0,  
  77.               1 - float(random.randint(1, 10)) / 100,  
  78.               float(random.randint(1, 2)) / 500,  
  79.               0.001,  
  80.               float(random.randint(1, 2)) / 500
  81.               ]  
  82.     img = img.transform(size, Image.PERSPECTIVE, params) # 创建扭曲  
  83.  
  84.     img = img.filter(ImageFilter.EDGE_ENHANCE_MORE) # 滤镜,边界加强(阈值更大)  
  85.  
  86.     return img,strs  
  87.  
  88. app = Flask(__name__)  
  89.  
  90. @app.route('/')  
  91. def index():  
  92.     return 'test'
  93.  
  94. @app.route('/code/')  
  95. def get_code():  
  96.     #把strs发给前端,或者在后台使用session保存  
  97.     code_img,strs = create_validate_code()  
  98.     buf = StringIO.StringIO()  
  99.     code_img.save(buf,'JPEG',quality=70)  
  100.  
  101.     buf_str = buf.getvalue()  
  102.     response = app.make_response(buf_str)  
  103.     response.headers['Content-Type'] = 'image/jpeg'  
  104.     return response  
  105.  
  106. if __name__ == "__main__":  
  107.     app.run(host="localhost",port=18888,debug=True)
  108. #//python/7367

Reply to "Python code for generating image verification code"

Here you can reply to the paste above

captcha

https://burned.cc - Burn After Reading Website