Pillow
pip install Pillow
二、验证码一般是放在一个项目的工具方法中三、验证码的代码#!/usr/bin/env python# enCoding: utf-8from random import randint,choicefrom PIL import Image,ImageDraw,ImageFontfrom cStringIO import StringIOfrom string import printabledef pillow_test(): #设置选用的字体 Font_path = "utils/captcha/Font/Arial.ttf" Font_color = (randint(150, 200), randint(0, 150), randint(0, 150)) line_color = (randint(0, 150), randint(0, 150), randint(150, 200)) point_color = (randint(0, 150), randint(50, 150), randint(150, 200)) #设置验证码的宽与高 wIDth, height = 100, 34 image = Image.new("RGB",(wIDth, height),(200,200,200)) Font = ImageFont.truetype(Font_path,height - 10) draw = ImageDraw.Draw(image) #生成验证码 text = "".join([choice(printable[:62]) for i in xrange(4)]) Font_wIDth, Font_height = Font.getsize(text) #把验证码写在画布上 draw.text((10, 10), text, Font=Font, fill=Font_color) #绘制干扰线 for i in xrange(0, 5): draw.line(((randint(0, wIDth), randint(0, height)), (randint(0, wIDth), randint(0, height))), fill=line_color, wIDth=2) # 绘制点 for i in xrange(randint(100, 1000)): draw.point((randint(0, wIDth), randint(0, height)), fill=point_color) #输出 out = StringIO() image.save(out, format='jpeg') content = out.getvalue() out.close() return text, content
四、创建一个视图返回验证码(基于tornado)#获取图像验证码class TestHandler(BaseHandler): def get(self): text, img = pillow_test() #设置头信息 self.set_header("Content-Type", "image/jpg") self.write(img)
五、直接在前端页面中的img
中使用这个视图 总结 以上是内存溢出为你收集整理的Python-web开发验证码的制作全部内容,希望文章能够帮你解决Python-web开发验证码的制作所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)