comparison data_generation/transformations/pycaptcha/Captcha/Visual/Text.py @ 184:81f8466dc121

Transient exception handling in captchas (ie. lorsque le NFS est temporairement inaccessible)
author boulanni <nicolas_boulanger@hotmail.com>
date Sat, 27 Feb 2010 19:26:26 -0500
parents 1f5937e9e530
children
comparison
equal deleted inserted replaced
183:992ca8035a4d 184:81f8466dc121
37 fileName = File.RandomFileFactory.pick(self) 37 fileName = File.RandomFileFactory.pick(self)
38 size = int(random.uniform(self.minSize, self.maxSize) + 0.5) 38 size = int(random.uniform(self.minSize, self.maxSize) + 0.5)
39 return (fileName, size) 39 return (fileName, size)
40 40
41 # Predefined font factories 41 # Predefined font factories
42 defaultFontFactory = FontFactory(25, "allfonts") 42 defaultFontFactory = FontFactory(25, "/Tmp/allfonts")
43 #defaultFontFactory = FontFactory((30, 40), "vera") 43 #defaultFontFactory = FontFactory((30, 40), "vera")
44 44
45 class TextLayer(Visual.Layer): 45 class TextLayer(Visual.Layer):
46 """Represents a piece of text rendered within the image. 46 """Represents a piece of text rendered within the image.
47 Alignment is given such that (0,0) places the text in the 47 Alignment is given such that (0,0) places the text in the
75 self.textColor = textColor 75 self.textColor = textColor
76 self.borderSize = borderSize 76 self.borderSize = borderSize
77 self.borderColor = borderColor 77 self.borderColor = borderColor
78 78
79 def render(self, img): 79 def render(self, img):
80 font = ImageFont.truetype(*self.font) 80
81 i=1
82 while True:
83 try:
84 font = ImageFont.truetype(*self.font)
85 break
86 except:
87 print "try#", i, self.font
88 i += 1
89 if i>10: raise
90
81 textSize = font.getsize(self.text) 91 textSize = font.getsize(self.text)
82 draw = ImageDraw.Draw(img) 92 draw = ImageDraw.Draw(img)
83 93
84 # Find the text's origin given our alignment and current image size 94 # Find the text's origin given our alignment and current image size
85 x = int((img.size[0] - textSize[0] - self.borderSize*2) * self.alignment[0] + 0.5) 95 x = int((img.size[0] - textSize[0] - self.borderSize*2) * self.alignment[0] + 0.5)