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)

验证码效果图如下:

验证码captcha

完整的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下可以正常使用,要注意扩展名的大小写。

评论关闭