google-code-export / django-simple-captcha

Automatically exported from code.google.com/p/django-simple-captcha
MIT License
0 stars 0 forks source link

Punctuation should be grouped with other letters for readability. #39

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
When using the word challenge with a custom wordlist which contains punctuation 
at the ends of words it can be impossible to make out the captcha because 
punctuation is floating at odd positions ( , can be confused with ' ).
If punctuation exists other than at the first position in the word this can be 
solved by appending punctuation characters to the previous letter during the 
rendering process.

The patch below does this as well as introducing a new setting 
CAPTCHA_PUNCTUATION which is a list of characters which should be joined with 
the previous letter and increases the image size to 1.2 * the font height to 
ensure that the entire letter(+punctuation) is displayed when rotated.

Index: captcha/views.py
===================================================================
--- captcha/views.py    (revision 58)
+++ captcha/views.py    (working copy)
@@ -23,7 +23,7 @@
         font = ImageFont.load(settings.CAPTCHA_FONT_PATH)

     size = font.getsize(text)
-    size = (size[0]*2,size[1])
+    size = (size[0]*2,size[1]*1.2)
     image = Image.new('RGB', size , settings.CAPTCHA_BACKGROUND_COLOR)

     try:
@@ -34,7 +34,14 @@

     xpos = 2
+    
+    charlist = []
     for char in text:
+        if char in settings.CAPTCHA_PUNCTUATION and len(charlist) >= 1:
+                charlist[-1]+= char
+        else:
+            charlist.append(char)
+    for char in charlist:
         fgimage = Image.new('RGB', size, settings.CAPTCHA_FOREGROUND_COLOR)
         charimage = Image.new('L', font.getsize(' %s '%char), '#000000')
         chardraw = ImageDraw.Draw(charimage)
Index: captcha/conf/settings.py
===================================================================
--- captcha/conf/settings.py    (revision 58)
+++ captcha/conf/settings.py    (working copy)
@@ -10,6 +10,7 @@
 CAPTCHA_NOISE_FUNCTIONS = getattr(settings,'CAPTCHA_NOISE_FUNCTIONS', ('captcha.helpers.noise_arcs','captcha.helpers.noise_dots',))
 CAPTCHA_FILTER_FUNCTIONS = getattr(settings,'CAPTCHA_FILTER_FUNCTIONS',('captcha.helpers.post_smooth',))
 CAPTCHA_WORDS_DICTIONARY = getattr(settings,'CAPTCHA_WORDS_DICTIONARY', '/usr/share/dict/words')
+CAPTCHA_PUNCTUATION = getattr(settings, 'CAPTCHA_PUNCTUATION', '''_"',.;:-''')
 CAPTCHA_FLITE_PATH = getattr(settings,'CAPTCHA_FLITE_PATH',None)
 CAPTCHA_TIMEOUT = getattr(settings, 'CAPTCHA_TIMEOUT', 5) # Minutes
 CAPTCHA_LENGTH = int(getattr(settings, 'CAPTCHA_LENGTH', 4)) # Chars

Original issue reported on code.google.com by solar.ge...@googlemail.com on 25 Oct 2010 at 7:27

GoogleCodeExporter commented 9 years ago
Nice addition, thank you. Implemented in r60.

Original comment by mbonetti on 9 Feb 2011 at 3:41