annotate data_generation/transformations/pycaptcha/Captcha/Visual/Base.py @ 550:662299f265ab

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