python生成验证码(webpy完整验证码类),pythonwebpy,python生成验证码可
python生成验证码(webpy完整验证码类),pythonwebpy,python生成验证码可
python生成验证码可以使用成熟的wheezy.captcha,首先需要安装PIL包 easy_install PIL
, 然后需要安装wheezy.captcha easy_install wheezy.captcha
安装之后生成验证码就很简单了:
captcha_image_t = captcha(drawings=[ background(), text(fonts=[ path.join(_fontsDir,'78640___.ttf'), path.join(_fontsDir,'andyb.ttf')], drawings=[ warp(), rotate(), offset() ]), curve(), noise(), smooth() ]) chars_t = random.sample(_chars, 4) image_t = captcha_image_t(chars_t) image_t.save('test.jpeg','jpeg',quality=75)
验证码效果图如下:
完整的webpy生成验证码类实现如下:
#!/usr/bin/env python# coding: utf-8__author__ = 'byrx.net'from wheezy.captcha.image import captchafrom wheezy.captcha.image import backgroundfrom wheezy.captcha.image import curvefrom wheezy.captcha.image import noisefrom wheezy.captcha.image import smoothfrom wheezy.captcha.image import textfrom wheezy.captcha.image import offsetfrom wheezy.captcha.image import rotatefrom wheezy.captcha.image import warpimport randomimport webfrom os import pathtry: from cStringIO import StringIOexcept: from StringIO import StringIOif __name__ == '__main__': import os,sys webPath = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) sys.path.insert(0,webPath)from run import session_controllersDir = path.abspath(path.dirname(__file__))_webDir = path.dirname(_controllersDir)_fontsDir = path.join(path.dirname(_webDir),'fonts')_chars = 'ABCDEFJHJKLMNPQRSTUVWXY3456789'SESSION_KEY_CAPTCHA = 'captcha'def isValidCaptcha(captchaInputName='captcha'): userInputVal = web.input().get(captchaInputName) if not userInputVal: return False correctVal = session[SESSION_KEY_CAPTCHA] return userInputVal.upper() == correctValclass Captcha: '''验证码''' def GET(self): captcha_image = captcha(drawings=[ background(), text(fonts=[ path.join(_fontsDir,'78640___.ttf'), path.join(_fontsDir,'andyb.ttf')], drawings=[ warp(), rotate(), offset() ]), curve(), noise(), smooth() ]) chars = random.sample(_chars, 4) session[SESSION_KEY_CAPTCHA] = ''.join(chars) image = captcha_image(chars) out = StringIO() image.save(out,"jpeg",quality=75) web.header('Content-Type','image/jpeg') return out.getvalue()if __name__ == '__main__': print _fontsDir captcha_image_t = captcha(drawings=[ background(), text(fonts=[ path.join(_fontsDir,'78640___.ttf'), path.join(_fontsDir,'andyb.ttf')], drawings=[ warp(), rotate(), offset() ]), curve(), noise(), smooth() ]) chars_t = random.sample(_chars, 4) image_t = captcha_image_t(chars_t) image_t.save('test.jpeg','jpeg',quality=75)
程序中使用的字体文件要放在web目录同级的fonts目录下,程序中的两个字体文件都是复制的windows字体,在linux下可以正常使用,要注意扩展名的大小写。
相关内容
- python小数除法取整数部分,python小数除法整数,python中的
- python获得url的顶级域名,pythonurl顶级域名,如下代码to
- python使用ctypes模块调用windowsapi获取系统版本,ctypeswi
- python解决约瑟夫环问题,python约瑟夫环,100人围成一圈,
- python统计代码行数代码,python统计行数,[Python]代码#实
- 原创Python数代码行数代码,原创python代码行数,如下py
- Python逐块读取大文件行数的代码,python逐行数代码,py
- python二分查找,python二分,python二分查找[P
- python实现的堆排序算法代码,python堆排序算法,def heap
- Python之SMTP 发邮件,pythonsmtp发邮件,这段代码解释了SM
评论关闭