CAPTCHA image to achieve

  . 1  # ! / Usr / bin / the env Python 
  2  # - * - Coding: UTF-. 8 - * - 
  . 3  
  . 4  Import Random
   . 5  from the PIL Import Image, ImageDraw, ImageFont, ImageFilter
   . 6  
  . 7 _letter_cases = " abcdefghjkmnpqrstuvwxy "   # lowercase removed possible interference I, L, O, Z 
  . 8 _upper_cases _letter_cases.upper = ()   # uppercase 
  . 9 _numbers = '' .join (Map (STR, Range (. 3, 10)))   # number 
10 init_chars = '' .join ((_letter_cases, _upper_cases, _numbers))
 11 
 12 
 13 def create_validate_code(size=(120, 30),
 14                          chars=init_chars,
 15                          img_type="GIF",
 16                          mode="RGB",
 17                          bg_color=(255, 255, 255),
 18                          fg_color=(0, 0, 255),
 19                          font_size=18,
 20                          font_type="Monaco.ttf",
 21                          . 4 = length ,
 22 is                           draw_lines = True,
 23 is                           n_line = (. 1, 2 ),
 24                           draw_points = True,
 25                           point_chance = 2 ):
 26 is      "" " 
27      @todo: generating a verification code image
 28      @param size: size of the picture, format (width, height), the default is (120, 30)
 29      @param chars: a collection of characters allowed, the format string
 30      @param img_type: save the image format, the default for GIF, optional for the GIF, JPEG, TIFF , PNG
 31 is      @param mODE: image mode default to the RGB
 32      @param bg_color: background color, the default is a white
 33     @param fg_color: foreground color, character color codes, default blue 0000FF #
 34 is      @param FONT_SIZE: font size codes
 35      @param font_type: font codes, default ae_AlArabiya.ttf
 36      @param length: one character codes number
 37 [      @param draw_lines: whether interference draw line
 38 is      @param n_lines: Article interference line number range, tuple format, default (1, 2), only effective to True draw_lines
 39      @param draw_points: whether points of interference Videos
 40      @param point_chance: the probability of occurrence of interference points, the size of the range [0, 100]
 41 is      @return: [0]: the PIL image example
 42 is      @return: [. 1]: verification code picture string
 43 is      "" " 
44 is  
45      width , = height size   # width and height of 
46      # to create graphics 
47     = IMG Image.new (MODE, size, bg_color)
 48      Draw = ImageDraw.Draw (IMG)   # Create Brush 
49  
50      DEF GET_CHARS ():
 51 is          "" " generated string for the given length, returns a list format " "" 
52          return random.sample (chars, length)
 53 is  
54 is      DEF create_lines ():
 55          "" " draw line interference " "" 
56 is          line_num the random.randint = (* n_line)   # interference number of lines 
57 is  
58          for I in Range (line_num):
 59              # starting point 
60             = the begin (the random.randint (0, size [0]), the random.randint (0, size [. 1 ]))
 61 is              # end point 
62 is              End = (the random.randint (0, size [0]), the random.randint (0, size [. 1 ]))
 63 is              draw.line ([the begin, End], Fill = (0, 0, 0))
 64  
65      DEF create_points ():
 66          "" " drawing interference point " "" 
67          Chance = min (100, max (0, int (point_chance)))   # size limit [0, 100] 
68  
69          for W in Range (width):
 70              for H in Range (height):
 71 is                 the random.randint = tmp (0, 100 )
 72                  IF tmp> 100 - Chance:
 73 is                      draw.point ((W, H), Fill = (0, 0, 0))
 74  
75      DEF create_strs ():
 76          "" " rendering the character codes "" " 
77          c_chars = GET_CHARS ()
 78          STRs = ' % S ' % '  ' .join (c_chars)   # after each character separated by a space 
79  
80          font = ImageFont.truetype (font_type, FONT_SIZE)
 81          font_width, font_height = font.getsize(strs)
 82 
 83         draw.text(((width - font_width) / 3, (height - font_height) / 3),
 84                   strs, font=font, fill=fg_color)
 85 
 86         return ''.join(c_chars)
 87 
 88     if draw_lines:
 89         create_lines()
 90     if draw_points:
 91         create_points()
 92     strs = create_strs()
 93 
 94     # 图形扭曲参数
 95     params = [1 - float(random.randint(1, 2)) / 100,
 96               0,
 97                0,
 98                0,
 99                . 1 - a float (the random.randint (. 1, 10)) / 100 ,
 100                a float (the random.randint (. 1, 2)) / 500 ,
 101                from 0.001 ,
 102                a float (the random.randint ( . 1, 2)) / 500
 103                ]
 104      IMG = img.transform (size, Image.PERSPECTIVE, the params)   # create distortions 
105  
106      IMG = img.filter (ImageFilter.EDGE_ENHANCE_MORE)   # filters, reinforcing border (higher threshold) 
107  
108      return IMG, STRs

how to use:

1 # 生成验证码图片
2 def get_check_code(request):
3     f = BytesIO()
4     img, code = check_code.create_validate_code()
5     request.session["check_code"] = code
6     img.save(f, "PNG")
7     return HttpResponse(f.getvalue())

Click on the image to replace the front-end verification code Click event:

1     function reload_check_code(th) {
2         th.src = th.src + "?";
3     }

Guess you like

Origin www.cnblogs.com/sun-10387834/p/12537561.html