# 图片验证码@user_db.route("/image")defimage():
uuid=request.args.get('uuid')
name,text,image=Captcha().generate_captcha()
res=make_response(image)
res.headers['Content-Type']='image/jpg'# 存入redi
r.set_str(uuid,text)return res
封装
#!/usr/bin/env python# -*- coding: utf-8 -*-# refer to `https://bitbucket.org/akorn/wheezy.captcha`import random
import string
import os.path
from io import BytesIO
from PIL import Image
from PIL import ImageFilter
from PIL.ImageDraw import Draw
from PIL.ImageFont import truetype
classBezier:def__init__(self):
self.tsequence =tuple([t /20.0for t inrange(21)])
self.beziers ={
}defpascal_row(self, n):""" Returns n-th row of Pascal's triangle
"""
result =[1]
x, numerator =1, n
for denominator inrange(1, n //2+1):
x *= numerator
x /= denominator
result.append(x)
numerator -=1if n &1==0:
result.extend(reversed(result[:-1]))else:
result.extend(reversed(result))return result
defmake_bezier(self, n):""" Bezier curves:
http://en.wikipedia.org/wiki/B%C3%A9zier_curve#Generalization
"""try:return self.beziers[n]except KeyError:
combinations = self.pascal_row(n -1)
result =[]for t in self.tsequence:
tpowers =(t ** i for i inrange(n))
upowers =((1- t)** i for i inrange(n -1,-1,-1))
coefs =[c * a * b for c, a, b inzip(combinations,
tpowers, upowers)]
result