Changeset 682

Show
Ignore:
Timestamp:
12/04/06 09:20:10
Author:
crchemist
Message:

Some optimizations.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • qPloneCaptchas/trunk/config.py

    r681 r682  
    1616DEFAULT_BG = 'gray' 
    1717DEFAULT_FONT_COLOR = 'black' 
    18 DEFAULT_PERIOD = 0.10 
     18DEFAULT_PERIOD = 0.1 
    1919DEFAULT_AMPLITUDE = 5 
    2020DEFAULT_OFFSET = (0.5, 0.5) 
  • qPloneCaptchas/trunk/utils.py

    r681 r682  
    77from DateTime import DateTime 
    88import re 
     9import math 
    910try: 
    1011    import Crypto.Cipher.DES as Crypto 
     
    1516    return md5.new(s).hexdigest().upper() 
    1617 
     18def getTransform(x, y, a, p, o): 
     19    return (math.sin( (y+o[0])*p )*a + x, math.sin( (x+o[1])*p )*a + y) 
     20 
    1721def gen_captcha(**kwargs): 
    1822    """Generate a captcha image""" 
     
    2226    import ImageDraw 
    2327    import ImageFilter 
    24     import random, math 
     28    import random 
    2529    from PIL import ImageFile as pyImageFile 
    2630    import sys 
     
    2832    from cStringIO import StringIO 
    2933 
    30     if kwargs.has_key('text'): 
    31         text = kwargs['text'] 
    32     else: 
    33         text = None 
    34  
    35     if kwargs.has_key('size'): 
    36         fnt_sz = kwargs['size'] 
    37     else: 
    38         fnt_sz = DEFAULT_IMAGE_SIZE 
    39  
    40     if kwargs.has_key('bkground'): 
    41         bkground = kwargs['bkground'] 
    42     else: 
    43         bkground = DEFAULT_BG 
    44  
    45     if kwargs.has_key('font_color'): 
    46         font_color = kwargs['font_color'] 
    47     else: 
    48         font_color = DEFAULT_FONT_COLOR 
    49  
    50     if kwargs.has_key('distortion'): 
    51         distortion = kwargs['distortion'] 
    52     else: 
    53         distortion = DEFAULT_DISTORTION 
     34    text = kwargs.get('text', None) 
     35    fnt_sz = kwargs.get('size', DEFAULT_IMAGE_SIZE) 
     36    bkground = kwargs.get('bkground', DEFAULT_BG) 
     37    font_color = kwargs.get('font_color', DEFAULT_FONT_COLOR) 
     38    distortion = kwargs.get('distortion', DEFAULT_DISTORTION) 
    5439 
    5540    period = distortion[0] 
    5641    amplitude = distortion[1] 
    5742    offset = distortion[2] 
    58  
    59     def getTransform(image): 
    60         return (lambda x, y, 
    61                 a = amplitude, 
    62                 p = period, 
    63                 o = offset: 
    64                 (math.sin( (y+o[0])*p )*a + x, 
    65                  math.sin( (x+o[1])*p )*a + y)) 
    6643 
    6744    outFile = StringIO() 
     
    8764    draw.text((x,y), text, font=font, fill=font_color) 
    8865 
    89 #------------------------------render       Distortion3 ----------------------- 
     66#------------------------------render       Distortion ----------------------- 
    9067    r = 1 
    9168    xPoints = image.size[0] / r + 2 
    9269    yPoints = image.size[1] / r + 2 
    93     f = getTransform(image) 
    9470 
    9571    # Create a list of arrays with transformed points 
     
    10076        yRow = [] 
    10177        for i in xrange(xPoints): 
    102             x, y = f(i*r, j*r
     78            x, y = getTransform(i*r, j*r, amplitude, period, offset
    10379 
    10480            # Clamp the edges so we don't get black undefined areas