/[glsr]/trunk/core/captcha.py
Gentoo

Contents of /trunk/core/captcha.py

Parent Directory Parent Directory | Revision Log Revision Log


Revision 87 - (show annotations) (download) (as text)
Wed Jul 20 19:59:04 2005 UTC (13 years, 1 month ago) by hadfield
File MIME type: text/x-python
File size: 1212 byte(s)
split the captcha code out of account.py (mostly)
1 # Copyright 2005 Gentoo Foundation
2 # Distributed under the terms of the GNU General Public License v2
3 #
4
5 """Returns a captcha image to the calling module."""
6
7 __revision__ = "$Id$"
8 __authors__ = ["Ian Leitch <port001@gentoo.org>",
9 "Scott Hadfield <hadfield@gentoo.org>"]
10 __modulename__ = "captcha"
11
12 import os
13 from random import Random
14 import StringIO
15
16 import Image
17 import ImageDraw
18 import ImageFont
19 import ImageFilter
20 import ImageColor
21
22 def gen_captcha(code, font = "verdana.ttf"):
23 """Generate a random captcha image."""
24
25 rand = Random()
26 colour = ImageColor.getcolor('rgb(221,218,236)', 'RGB')
27 im = Image.new('RGB', (200, 80), colour)
28 font = ImageFont.truetype(font, 40)
29 draw = ImageDraw.Draw(im)
30 y_pos = 15
31
32 # Draw each character
33 for i in range(0, 6):
34
35 (char_width, char_height) = font.getsize(code[i])
36 # Pick a random X co-ord
37 x_pos = rand.randint(0, 30)
38 draw.text((y_pos, x_pos), code[i], font=font, fill=(0,0,0))
39 y_pos = y_pos + 30
40
41 # Blur the text
42 im = im.filter(ImageFilter.BLUR)
43
44 image_data = StringIO.StringIO()
45 im.save(image_data, 'JPEG')
46 image_data.seek(0)
47
48 return image_data.read()

Properties

Name Value
svn:keywords Id

  ViewVC Help
Powered by ViewVC 1.1.20