Mercurial > ift6266
comparison pycaptcha/Captcha/Visual/Base.py~ @ 87:4775b4195b4b
code pour la generation de captchas
author | goldfinger |
---|---|
date | Thu, 11 Feb 2010 05:09:46 -0500 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
86:b3d76ebf2fac | 87:4775b4195b4b |
---|---|
1 """ Captcha.Visual.BAse | |
2 | |
3 Base classes for visual CAPTCHAs. We use the Python Imaging Library | |
4 to manipulate these images. | |
5 """ | |
6 # | |
7 # PyCAPTCHA Package | |
8 # Copyright (C) 2004 Micah Dowty <micah@navi.cx> | |
9 # | |
10 | |
11 import Captcha | |
12 import Image | |
13 | |
14 __all__ = ['ImageCaptcha', 'Layer'] | |
15 | |
16 | |
17 class ImageCaptcha(Captcha.BaseCaptcha): | |
18 """Base class for image-based CAPTCHA tests. | |
19 The render() function generates the CAPTCHA image at the given size by | |
20 combining Layer instances from self.layers, which should be created by | |
21 the subclass-defined getLayers(). | |
22 """ | |
23 defaultSize = (256,96) | |
24 | |
25 def __init__(self, *args, **kwargs): | |
26 Captcha.BaseCaptcha.__init__(self) | |
27 self._layers = self.getLayers(*args, **kwargs) | |
28 | |
29 def getImage(self): | |
30 """Get a PIL image representing this CAPTCHA test, creating it if necessary""" | |
31 if not self._image: | |
32 self._image = self.render() | |
33 return self._image | |
34 | |
35 def getLayers(self): | |
36 """Subclasses must override this to return a list of Layer instances to render. | |
37 Lists within the list of layers are recursively rendered. | |
38 """ | |
39 return [] | |
40 | |
41 def render(self, size=None): | |
42 """Render this CAPTCHA, returning a PIL image""" | |
43 if size is None: | |
44 size = self.defaultSize | |
45 img = Image.new("RGB", size) | |
46 return self._renderList(self._layers, Image.new("RGB", size)) | |
47 | |
48 def _renderList(self, l, img): | |
49 for i in l: | |
50 if type(i) == tuple or type(i) == list: | |
51 img = self._renderList(i, img) | |
52 else: | |
53 img = i.render(img) or img | |
54 return img | |
55 | |
56 | |
57 class Layer(object): | |
58 """A renderable object representing part of a CAPTCHA. | |
59 The render() function should return approximately the same result, regardless | |
60 of the image size. This means any randomization must occur in the constructor. | |
61 | |
62 If the render() function returns something non-None, it is taken as an image to | |
63 replace the current image with. This can be used to implement transformations | |
64 that result in a separate image without having to copy the results back to the first. | |
65 """ | |
66 def render(self, img): | |
67 pass | |
68 | |
69 ### The End ### |