[captcha] improve a bit the redability of generated captcha images (closes #13500378) 3.20
authorDavid Douard <david.douard@logilab.fr>
Tue, 19 Jul 2016 13:50:56 +0200
branch3.20
changeset 11399 5149a4d4cf54
parent 11398 d71ce8b30a52
child 11400 5a366414d904
[captcha] improve a bit the redability of generated captcha images (closes #13500378)
web/captcha.py
--- a/web/captcha.py	Tue Jul 19 13:56:40 2016 +0200
+++ b/web/captcha.py	Tue Jul 19 13:50:56 2016 +0200
@@ -39,24 +39,24 @@
     adapted from http://code.activestate.com/recipes/440588/
     """
     # randomly select the foreground color
-    fgcolor = randint(0, 0xffff00)
-    # make the background color the opposite of fgcolor
-    bgcolor = fgcolor ^ 0xffffff
+    fgcolor = (randint(100, 256), randint(100, 256), randint(100, 256))
     # create a font object
     font = ImageFont.truetype(fontfile, fontsize)
     # determine dimensions of the text
     dim = font.getsize(text)
     # create a new image slightly larger that the text
-    img = Image.new('RGB', (dim[0]+5, dim[1]+5), bgcolor)
+    img = Image.new('RGB', (dim[0]+15, dim[1]+5), 0)
     draw = ImageDraw.Draw(img)
     # draw 100 random colored boxes on the background
     x, y = img.size
     for num in xrange(100):
+        fill = (randint(0, 100), randint(0, 100), randint(0, 100))
         draw.rectangle((randint(0, x), randint(0, y),
                         randint(0, x), randint(0, y)),
-                       fill=randint(0, 0xffffff))
+                       fill=fill)
     # add the text to the image
-    draw.text((3, 3), text, font=font, fill=fgcolor)
+    # we add a trailing space to prevent the last char to be truncated
+    draw.text((3, 3), text + ' ', font=font, fill=fgcolor)
     img = img.filter(ImageFilter.EDGE_ENHANCE_MORE)
     return img