<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ??碼云GVP開源項目 12k star Uniapp+ElementUI 功能強大 支持多語言、二開方便! 廣告
                ### 生成驗證碼 ``` import random,string # Image:畫布 # ImageDraw:畫筆 # ImageFont:畫筆的字體 # 相關庫安裝 # pip install pillow from PIL import Image,ImageDraw,ImageFont # Captcha驗證碼 class Captcha(object): # 生成幾位數的驗證碼 number = 4 # 驗證碼圖片的寬度和高度 size = (100,30) # 驗證字體大小 fontsize = 25 # 加入干擾線的條數 line_number = 2 # 構建一個驗證碼源文本 SOURCE = list(string.ascii_letters) for index in range(0,10): SOURCE.append(str(index)) # 填充顏色為150~255,顏色偏亮 # 填充顏色為0~100,顏色偏暗 # 隨機生成的顏色 @classmethod def __gene_random_color(cls,start=0,end=255): random.seed() # 隨機生成3個0~255的參數以元組的形式返回 return (random.randint(start,end),random.randint(start,end),random.randint(start,end)) # 隨機選擇一個字體 @classmethod def __gene_random_font(cls): fonts = [ 'Courgette-Regular.ttf', 'LHANDW.TTF', 'Lobster-Regular.ttf', 'verdana.ttf' ] # 隨機選擇一個字體 font = random.choice(fonts) return r'utils/captcha/'+font # 用來隨機生成一個字符串(包括英文和數字) @classmethod def gene_text(cls,number): # number是生成驗證碼的位數 return ''.join(random.sample(cls.SOURCE,number)) # 繪制干擾線 @classmethod def __gene_line(cls,draw,width,height): begin = (random.randint(0,width),random.randint(0,height)) end = (random.randint(0,width),random.randint(0,height)) draw.line([begin,end],fill=cls.__gene_random_color(),width=2) # 繪制干擾點 @classmethod def __gene_points(cls,draw,point_chance,width,height): # 大小限制在[0~100] chance = min(100,max(0,int(point_chance))) for w in range(width): for h in range(height): tmp = random.randint(0,100) if tmp > 100 - chance: draw.point((w,h),fill=cls.__gene_random_color()) # 生成驗證碼 @classmethod def gene_graph_captcha(cls): try: # 驗證碼圖片的寬和高 width,height = cls.size # 創建圖片 # R:red 0-255 # G:green 0-255 # B:blue 0-255 # A:Alpha 0-255 image = Image.new('RGBA',(width,height),cls.__gene_random_color(0,100)) # 驗證碼的字體 font = ImageFont.truetype(cls.__gene_random_font(),cls.fontsize) # 創建畫筆 draw = ImageDraw.Draw(image) # 生成字符串 text = cls.gene_text(cls.number) # 獲取字體的尺寸 font_width,font_height = font.getsize(text) # 填充字符串 # draw.text(x,y,fill) draw.text(((width-font_width)/2,(height-font_height)/2),text,font=font,fill=cls.__gene_random_color(150,255)) # 繪制干擾線 for x in range(0,cls.line_number): cls.__gene_line(draw,width,height) # 繪制噪點 cls.__gene_points(draw,10,width,height) except: Captcha.gene_graph_captcha() return (text,image) ``` ### 在視圖中生成驗證碼 ``` @bp.route('/captcha/') def graph_captcha(): try: # 獲取驗證碼 text,image = Captcha.gene_graph_captcha() # BytesIO,字節流 # 獲取圖像,需要存入二進制流中 out = BytesIO() # 保存在流中,并指定格式為png image.save(out,'png') out.seek(0) # response對象 resp = make_response(out.read()) resp.content_type = 'image/png' except: return graph_captcha() return resp ```
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看