python实现生成验证码的逻辑

olei 2,953 views 2

 假设我们有一个fonts的文件夹,里面有1.ttf,2.ttf,3.ttf三个字体文件

具体代码实现代码codes.py:

  1. # coding:utf8
  2. from PIL import ImageDraw, ImageColor, ImageFile, ImageFont, ImageFilter, Image
  3. import random
  4. import os
  5. import uuid
  6. class Codes:
  7.     # 定义随机字符
  8.     def random_chr(self):
  9.         num = random.randint(1, 3)
  10.         if num == 1:
  11.             char = random.randint(48, 57)
  12.         elif num == 2:
  13.             char = random.randint(97, 122)
  14.         else:
  15.             char = random.randint(65, 90)
  16.         return chr(char)
  17.     # 定义干扰字符
  18.     def random_dis(self):
  19.         arr = ["~", "^", "_", "."]
  20.         return arr[random.randint(0, len(arr) - 1)]
  21.     # 定义干扰字符的颜色
  22.     def random_color1(self):
  23.         return (random.randint(64, 255), random.randint(64, 255), random.randint(64, 255))
  24.     def random_color2(self):
  25.         return (random.randint(32, 127), random.randint(32, 127), random.randint(32, 127))
  26.     # 生成验证码
  27.     def create_code(self):
  28.         width = 200
  29.         height = 60
  30.         # 创建一个画布图片
  31.         image = Image.new("RGB", (width, height), (192, 192, 192))
  32.         # 创建font对象,定义字体以及大小
  33.         font_name = random.randint(1, 3)
  34.         font_file = os.path.join(os.path.dirname(__file__), "fonts") + "/" + "%d.ttf" % font_name
  35.         font = ImageFont.truetype(font_file, 30)
  36.         # 填充像素点
  37.         draw = ImageDraw.Draw(image)
  38.         for x in range(0, width, 5):
  39.             for y in range(0, height, 5):
  40.                 draw.point((x, y), fill=self.random_color1())
  41.         # 填充干扰字符
  42.         for v in range(0, width, 30):
  43.             dis = self.random_dis()
  44.             w = 5 + v
  45.             h = random.randint(5, 15)
  46.             draw.text((w, h), dis, font=font, fill=self.random_color1())
  47.         # 填充字符
  48.         chars = ""
  49.         for v in range(4):
  50.             c = self.random_chr()
  51.             chars += str(c)
  52.             h = random.randint(5, 15)
  53.             w = width / 4 * v + 10
  54.             draw.text((w, h), c, font=font, fill=self.random_color2())
  55.         # 模糊效果
  56.         image = filter(ImageFilter.BLUR)
  57.         # 保证图片名称的唯一性
  58.         image_name = "%s.jpg" % uuid.uuid4().hex
  59.         save_dir = os.path.join(os.path.dirname(__file__), "codes")
  60.         if not os.path.exists(save_dir):
  61.             os.makedirs(save_dir)
  62.         image.save(save_dir + '/' + image_name)
  63.         return dict(
  64.             img_name=image_name,
  65.             code=chars
  66.         )

 应用

flask项目中

  • 在定义路由的views.py文件中引用。自定义一个"/codes/"的路由,用来GET到验证码
  1. # 验证码
  2. @app.route('/codes/', methods=["GET"])
  3. def codes():
  4.     from codes import Codes
  5.     c = Codes()
  6.     info = c.create_code()
  7.     image = os.path.join(os.path.dirname(__file__), "static/code") + "/" + info["img_name"]
  8.     with open(image, "rb") as f:
  9.         image = f.read()
  10.     session["code"] = info["code"]
  11.     return Response(image, mimetype="jpeg")

 

上面读取到图片并显示出来了,将验证码的值给到session["code"]来存储,这样访问http:xxx.xx.xx./codes/就可以获取到验证码的图片,并保存在本地code的文件夹中

 

  • 然后在前端页面验证码图片显示的的地方引用,点击图片会重新切换生成一个
  1. <img alt="点击切换验证码" title="点击切换" src="/codes/" onclick="this.src='/codes/?' + Math.random()"
  2.              style="width: 180px; height:50px; margin-top: 6px;">
  • 在forms.py文件中存入session的code与code表单的输入的值进行对比判断
  1. # 自定义验证码验证功能
  2.     def validate_code(self,field):
  3.         code = field.data
  4.         # if not session.has_key("code"): python2写法
  5.         if "code" not in session:
  6.             raise ValidationError("没有验证码")
  7.         # if session.has_key("code") and session["code"].lower() != code.lower():python2写法
  8.         if "code" in session and session["code"].lower() != code.lower(): # 从views中获取session的"code"
  9.             raise ValidationError("验证码错误")

发表评论 取消回复
表情 图片 链接 代码

  1. 陈鑫威博客

    好文章,不错[aru_1]自己改改,整个小孟坤[aru_4]

分享