annotate pycaptcha/Captcha/Visual/Backgrounds.py @ 144:c958941c1b9d

merge
author XavierMuller
date Tue, 23 Feb 2010 18:16:55 -0500
parents 4775b4195b4b
children
rev   line source
87
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
1 """ Captcha.Visual.Backgrounds
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
2
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
3 Background layers for visual CAPTCHAs
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
4 """
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
5 #
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
6 # PyCAPTCHA Package
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
7 # Copyright (C) 2004 Micah Dowty <micah@navi.cx>
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
8 #
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
9
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
10 from Captcha.Visual import Layer, Pictures
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
11 import random, os
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
12 import ImageDraw, 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
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
15 class SolidColor(Layer):
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
16 """A solid color background. Very weak on its own, but good
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
17 to combine with other backgrounds.
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
18 """
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
19 def __init__(self, color="white"):
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
20 self.color = color
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
21
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
22 def render(self, image):
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
23 image.paste(self.color)
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
24
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
25
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
26 class Grid(Layer):
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
27 """A grid of lines, with a given foreground color.
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
28 The size is given in pixels. The background is transparent,
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
29 so another layer (like SolidColor) should be put behind it.
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
30 """
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
31 def __init__(self, size=16, foreground="black"):
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
32 self.size = size
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
33 self.foreground = foreground
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
34 self.offset = (random.uniform(0, self.size),
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
35 random.uniform(0, self.size))
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
36
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
37 def render(self, image):
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
38 draw = ImageDraw.Draw(image)
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
39
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
40 for i in xrange(image.size[0] / self.size + 1):
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
41 draw.line( (i*self.size+self.offset[0], 0,
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
42 i*self.size+self.offset[0], image.size[1]), fill=self.foreground)
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
43
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
44 for i in xrange(image.size[0] / self.size + 1):
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
45 draw.line( (0, i*self.size+self.offset[1],
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
46 image.size[0], i*self.size+self.offset[1]), fill=self.foreground)
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
47
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
48
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
49 class TiledImage(Layer):
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
50 """Pick a random image and a random offset, and tile the rendered image with it"""
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
51 def __init__(self, imageFactory=Pictures.abstract):
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
52 self.tileName = imageFactory.pick()
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
53 self.offset = (random.uniform(0, 1),
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
54 random.uniform(0, 1))
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
55
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
56 def render(self, image):
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
57 tile = Image.open(self.tileName)
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
58 for j in xrange(-1, int(image.size[1] / tile.size[1]) + 1):
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
59 for i in xrange(-1, int(image.size[0] / tile.size[0]) + 1):
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
60 dest = (int((self.offset[0] + i) * tile.size[0]),
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
61 int((self.offset[1] + j) * tile.size[1]))
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
62 image.paste(tile, dest)
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
63
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
64
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
65 class CroppedImage(Layer):
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
66 """Pick a random image, cropped randomly. Source images should be larger than the CAPTCHA."""
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
67 def __init__(self, imageFactory=Pictures.nature):
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
68 self.imageName = imageFactory.pick()
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
69 self.align = (random.uniform(0,1),
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
70 random.uniform(0,1))
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
71
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
72 def render(self, image):
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
73 i = Image.open(self.imageName)
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
74 image.paste(i, (int(self.align[0] * (image.size[0] - i.size[0])),
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
75 int(self.align[1] * (image.size[1] - i.size[1]))))
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
76
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
77
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
78 class RandomDots(Layer):
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
79 """Draw random colored dots"""
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
80 def __init__(self, colors=("white", "black"), dotSize=4, numDots=400):
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
81 self.colors = colors
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
82 self.dotSize = dotSize
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
83 self.numDots = numDots
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
84 self.seed = random.random()
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
85
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
86 def render(self, image):
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
87 r = random.Random(self.seed)
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
88 for i in xrange(self.numDots):
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
89 bx = int(r.uniform(0, image.size[0]-self.dotSize))
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
90 by = int(r.uniform(0, image.size[1]-self.dotSize))
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
91 image.paste(r.choice(self.colors), (bx, by,
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
92 bx+self.dotSize-1,
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
93 by+self.dotSize-1))
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
94
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
95 ### The End ###