Created
August 13, 2013 13:21
-
-
Save yanmhlv/6221007 to your computer and use it in GitHub Desktop.
captcha
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# coding: utf-8 | |
from PIL import Image, ImageDraw, ImageFont | |
from random import randint, choice | |
import StringIO, string | |
def captcha(): | |
key = ''.join( [choice(string.ascii_lowercase + string.digits) for i in xrange(5)] ) | |
img = Image.new('RGB', (100,30), 0xffffff ) | |
draw = ImageDraw.Draw(img) | |
for i in xrange(40): | |
draw.line( [(randint(0,100),randint(0,30)),(randint(0,100),randint(0,30))], randint(0, 0xffffff), 1) | |
font = ImageFont.truetype('/usr/share/fonts/truetype/freefont/FreeMono.ttf', 32) | |
draw.text( (0,0), key, 0, font) | |
f = StringIO.StringIO() | |
img.save(f, "JPEG") | |
raw = f.getvalue() | |
return key,raw | |
#@get('/captcha') | |
#@app.route('/captcha') | |
def retcapcha(): | |
key, raw = captcha() | |
response.set_cookie('capcha_hash', hash(key)) | |
db.save(key) | |
response.headers['Content-Type'] = 'image/jpeg' | |
return key | |
if __name__ == '__main__': | |
print captcha |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
great!