# HG changeset patch # User David Douard # Date 1468929056 -7200 # Node ID 5149a4d4cf5434f4d4018b378c6c0cb01e333441 # Parent d71ce8b30a52c77a7b8296d0165e7c900464c094 [captcha] improve a bit the redability of generated captcha images (closes #13500378) diff -r d71ce8b30a52 -r 5149a4d4cf54 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