validate_code.py 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. #*************************************************************************
  2. # Copyright © 2015 JiangLin. All rights reserved.
  3. # File Name: cd.py
  4. # Author:JiangLin
  5. # Mail:xiyang0807@gmail.com
  6. # Created Time: 2015-11-22 04:11:03
  7. #*************************************************************************
  8. #!/usr/bin/env python
  9. # -*- coding=UTF-8 -*-
  10. import random
  11. from PIL import Image, ImageDraw, ImageFont, ImageFilter
  12. from flask import session
  13. from io import BytesIO
  14. class ValidateCode(object):
  15. _letter_cases = "abcdefghjkmnpqrstuvwxy" # 小写字母,去除可能干扰的i,l,o,z
  16. _upper_cases = _letter_cases.upper() # 大写字母
  17. _numbers = ''.join(map(str, range(3, 10))) # 数字
  18. init_chars = ''.join((_letter_cases, _upper_cases, _numbers))
  19. fontType = "/usr/share/fonts/TTF/DejaVuSans.ttf"
  20. def create_validate_code(self, size=(120, 30),
  21. chars=init_chars,
  22. img_type="GIF",
  23. mode="RGB",
  24. bg_color=(255, 255, 255),
  25. fg_color=(0, 0, 255),
  26. font_size=18,
  27. font_type=fontType,
  28. length=4,
  29. draw_lines=True,
  30. n_line=(1, 2),
  31. draw_points=True,
  32. point_chance=2):
  33. width, height = size # 宽, 高
  34. img = Image.new(mode, size, bg_color) # 创建图形
  35. draw = ImageDraw.Draw(img) # 创建画笔
  36. if draw_lines:
  37. self.create_lines(draw, n_line, width, height)
  38. if draw_points:
  39. self.create_points(draw, point_chance, width, height)
  40. strs = self.create_strs(draw,
  41. chars,
  42. length,
  43. font_type,
  44. font_size,
  45. width,
  46. height,
  47. fg_color)
  48. # 图形扭曲参数
  49. params = [1 - float(random.randint(1, 2)) / 100,
  50. 0,
  51. 0,
  52. 0,
  53. 1 - float(random.randint(1, 10)) / 100,
  54. float(random.randint(1, 2)) / 500,
  55. 0.001,
  56. float(random.randint(1, 2)) / 500
  57. ]
  58. img = img.transform(size, Image.PERSPECTIVE, params) # 创建扭曲
  59. img = img.filter(ImageFilter.EDGE_ENHANCE_MORE) # 滤镜,边界加强(阈值更大)
  60. return img, strs
  61. def create_lines(self, draw, n_line, width, height):
  62. '''绘制干扰线'''
  63. line_num = random.randint(n_line[0], n_line[1]) # 干扰线条数
  64. for i in range(line_num):
  65. # 起始点
  66. begin = (random.randint(0, width), random.randint(0, height))
  67. # 结束点
  68. end = (random.randint(0, width), random.randint(0, height))
  69. draw.line([begin, end], fill=(0, 0, 0))
  70. def create_points(self, draw, point_chance, width, height):
  71. '''绘制干扰点'''
  72. chance = min(100, max(0, int(point_chance))) # 大小限制在[0, 100]
  73. for w in range(width):
  74. for h in range(height):
  75. tmp = random.randint(0, 100)
  76. if tmp > 100 - chance:
  77. draw.point((w, h), fill=(0, 0, 0))
  78. def create_strs(
  79. self,
  80. draw,
  81. chars,
  82. length,
  83. font_type,
  84. font_size,
  85. width,
  86. height,
  87. fg_color):
  88. '''绘制验证码字符'''
  89. '''生成给定长度的字符串,返回列表格式'''
  90. c_chars = random.sample(chars, length)
  91. strs = ' %s ' % ' '.join(c_chars) # 每个字符前后以空格隔开
  92. font = ImageFont.truetype(font_type, font_size)
  93. font_width, font_height = font.getsize(strs)
  94. draw.text(((width - font_width) / 3, (height - font_height) / 3), strs,
  95. font=font, fill=fg_color)
  96. return ''.join(c_chars)
  97. def start(self):
  98. code_img = self.create_validate_code()
  99. buf = BytesIO()
  100. code_img[0].save(buf, 'JPEG', quality=70)
  101. session['validate_code'] = code_img[1]
  102. return buf