04 codes third party package

  Installation Module

1 pip install flask
2 pip install pillow

  Block

  . 1  from Flask Import the Flask, make_response
   2  Import Random
   . 3  Import String
   . 4  from IO Import BytesIO
   . 5 App = the Flask ( the __name__ )
   . 6  
  . 7  from the PIL Import ImageDraw, Image, ImageFont
   . 8  
  . 9  
10  class Captcha:
 . 11     # number of codes generated 
12 is     Number. 4 =
 13 is     # picture width and height of 
14     size = (80, 30 )
 15     #Font size 
16     fontSize = 25
 . 17     # interference number of lines 
18 is     LINE_NUMBER = 2
 . 19  
20 is     the SOURCE = List (string.ascii_letters)
 21 is     SOURCE.extend (Map (STR, List (Range (0, 10 ))))
 22 is  
23 is     @classmethod
 24     DEF  __gen_line (CLS, draw, width, height):
 25        "" " 
26 is        drawn interference line
 27        " "" 
28        the begin = (the random.randint (0, width), the random.randint (0, height))
 29        End = ( the random.randint (0, width), the random.randint (0, height))
 30       draw.line (. [the begin, End], Fill = CLS __gen_random_color (), width = 2 )
 31 is  
32     @classmethod
 33 is     DEF  __gen_random_color (CLS, Start = 0, End = 255 ):
 34 is        "" " 
35        generates a random color
 36        the color is in the range of 255 ~ 0
 37 [        "" " 
38 is        random.seed ()
 39        return (
 40           the random.randint (Start, End),
 41 is           the random.randint (Start, End),
 42 is           the random.randint (Start, End ),
 43 is        )
 44 is  
45     @classmethod
 46    def __gen_points(cls, draw, point_chance, width, height):
 47       """
 48       绘制干扰点
 49       """
 50       chance = min(100, max(0, int(point_chance)))
 51       for w in range(width):
 52          for h in range(height):
 53             temp = random.randint(0, 100)
 54             if temp > 100 - chance:
 55                draw.point((w, h), fill=cls.__gen_random_color())
 56 
 57    @classmethod
 58    def __gen_random_font(cls):
 59       """
 60       采用随机字体
 61       :return:
 62       """
 63       fonts = ["consola.ttf", "consolab.ttf", "consolai.ttf"]
 64       font = random.choice(fonts)
 65       return "utils/captcha/" + font
 66 
 67    @classmethod
 68    def gen_text(cls, number):
 69       """
 70       A randomly generated string
 71 is        : param Number: Number of strings
 72        "" " 
73 is        return  " " .join (random.sample (cls.SOURCE, Number))
 74  
75     @classmethod
 76     DEF gen_graph_captcha (CLS):
 77        width, height = cls.size
 78        # A represents transparency 
79        Image Image.new = ( " the RGBA " , (width, height), CLS. __gen_random_color (0, 100 ))
 80        # Font 
81        font = ImageFont.truetype (CLS. __gen_random_font () , cls.fontsize)
82        # Create brush 
83        Draw = ImageDraw.Draw (Image)
 84        # generates a random string of 
85        text = cls.gen_text (cls.number)
 86        # font size 
87        font_width, font_height = font.getsize (text)
 88        # filled string 
89        draw.text (
 90           ((width - font_width) / 2, (height - font_height) / 2 ),
 91 is           text,
 92           font = font,
 93           . Fill = CLS __gen_random_color (150, 255 ),
 94       )
 95        # drawn interference line 
96        for X in Range (0, cls.line_number):
 97           . CLS __gen_line (Draw, width, height)
 98        # draw noise 
99        . CLS __gen_points (Draw, 10 , width, height)
 100  
101        return text, Image
 102  
103  
104 @ app.route ( ' / captcha / ' )
 105  DEF graph_captcha ():
 106     text, Image = Captcha.gen_graph_captcha ()
 107     OUT = BytesIO()
108    image.save(out, 'png')
109    out.seek(0)
110    resp = make_response(out.read())
111    resp.content_type = 'image/png'
112    return resp
113 
114 
115 if __name__ == '__main__':
116     app.run()

  Pattern codes

       

 

Guess you like

Origin www.cnblogs.com/a2534786642/p/11040699.html