<kbd id="afajh"><form id="afajh"></form></kbd>
<strong id="afajh"><dl id="afajh"></dl></strong>
    <del id="afajh"><form id="afajh"></form></del>
        1. <th id="afajh"><progress id="afajh"></progress></th>
          <b id="afajh"><abbr id="afajh"></abbr></b>
          <th id="afajh"><progress id="afajh"></progress></th>

          python 實(shí)現(xiàn)自動(dòng)生成驗(yàn)證碼圖片

          共 3116字,需瀏覽 7分鐘

           ·

          2021-04-30 08:50

          20f1e3f5818d3e906f0139d29f1c650b.webp

          思路

          基本思路是使用opencv來(lái)把隨機(jī)生成的字符,和隨機(jī)生成的線段,放到一個(gè)隨機(jī)生成的圖像中去。

          感謝各友的鼓勵(lì)與支持??????,往期文章都在最后梳理出來(lái)了(●'?'●)

          ??????????????????????????????????????????????????????????????????

          代碼一

          import cv2import numpy as npline_num = 10pic_num = 1000path = "./imgs/"def randcolor():            return (np.random.randint(0,255),np.random.randint(0,255),np.random.randint(0,255))def randchar():    return chr(np.random.randint(65,90))def randpos(x_start,x_end,y_start,y_end):    return (np.random.randint(x_start,x_end),            np.random.randint(y_start,y_end))img_heigth = 60img_width = 240for i in range(pic_num):    img_name = ""    #生成一個(gè)隨機(jī)矩陣,randint(low[, high, size, dtype])    img = np.random.randint(100,200,(img_heigth,img_width, 3), np.uint8)    #顯示圖像    #cv2.imshow("ranImg",img)    x_pos = 0    y_pos = 25    for i in range(4):        char = randchar()        img_name += char        cv2.putText(img,char,                    (np.random.randint(x_pos,x_pos + 50),np.random.randint(y_pos,y_pos + 35)),                     cv2.FONT_HERSHEY_SIMPLEX,                    1.5,                    randcolor(),                    2,                    cv2.LINE_AA)        x_pos += 45    #cv2.imshow("res",img)    #添加線段    for i in range(line_num):        img = cv2.line(img,                       randpos(0,img_width,0,img_heigth),                       randpos(0,img_width,0,img_heigth),                        randcolor(),                        np.random.randint(1,2))    #cv2.imshow("line",img)    cv2.imwrite(path + img_name + ".jpg",img)    #cv2.waitKey(0)                      #cv2.destroyAllWindows()

          結(jié)果

          c8edaedba6a02ef24ebbb243247c8070.webp


          代碼二

          import osimport random# captcha是用于生成驗(yàn)證碼圖片的庫(kù),可以 pip install captcha 來(lái)安裝它from captcha.image import ImageCaptchadef random_captcha_text(num):    # 驗(yàn)證碼列表    captcha_text = []    for i in range(10): # 0-9數(shù)字        captcha_text.append(str(i))    for i in range(65, 91): # 對(duì)應(yīng)從“A”到“Z”的ASCII碼        captcha_text.append(chr(i))    for i in range(97, 123): #對(duì)應(yīng)從“a”到“z”的ASCII碼        captcha_text.append(chr(i))    # 從list中隨機(jī)獲取6個(gè)元素,作為一個(gè)片斷返回    example = random.sample(captcha_text, num)    # 將列表里的片段變?yōu)樽址⒎祷?/span>    verification_code = ''.join(example)    return verification_code# 生成字符對(duì)應(yīng)的驗(yàn)證碼def generate_captcha_image():    image = ImageCaptcha()    #獲得隨機(jī)生成的驗(yàn)證碼    captcha_text = random_captcha_text(8)    #把驗(yàn)證碼列表轉(zhuǎn)為字符串    captcha_text = ''.join(captcha_text)    #生成驗(yàn)證碼    path='E:/pycharm/驗(yàn)證碼識(shí)別/code/test/'    if not os.path.exists(path):        print("目錄不存在!,已自動(dòng)創(chuàng)建" )        os.makedirs(path)    print("生成的驗(yàn)證碼的圖片為:", captcha_text)    image.write(captcha_text, path + captcha_text + '.png')if __name__ == '__main__':    number=1000    for i in range(number):        generate_captcha_image()

          結(jié)果

          b316329a0a7f9dc7c69cc3ecb4106554.webp


          「?? 感謝大家」

          如果你覺得這篇內(nèi)容對(duì)你挺有有幫助的話:

          1. 點(diǎn)贊支持下吧,讓更多的人也能看到這篇內(nèi)容(收藏不點(diǎn)贊,都是耍流氓 -_-)
          2. 歡迎在留言區(qū)與我分享你的想法,也歡迎你在留言區(qū)記錄你的思考過(guò)程。
          3. 覺得不錯(cuò)的話,也可以閱讀近期梳理的文章(感謝鼓勵(lì)與支持??????):


          老鐵,三連支持一下,好嗎?↓↓↓



          cbeb35e455488d20c1f0659d639a92ab.webp
          歡迎大家加入到知識(shí)星球這個(gè)大家庭,這里一定有與你志同道合的小伙伴,在這里大家可以一起交流,一起學(xué)習(xí),一同吹逼,一同玩耍。。。


          長(zhǎng)按按鈕? “識(shí)別二維碼”?關(guān)注我更多精彩內(nèi)容等著你哦

          0690fafc5e0cbb5fa1f3a9509392e17d.webp

          25d3576f590b6f7cc35cfa4d6a163cfa.webp

          點(diǎn)分享

          951d508139cdfd6bd993534bb3c96090.webp

          點(diǎn)點(diǎn)贊

          bcc0a63dda1839184250d95d7ce67ce5.webp

          點(diǎn)在看

          瀏覽 72
          點(diǎn)贊
          評(píng)論
          收藏
          分享

          手機(jī)掃一掃分享

          分享
          舉報(bào)
          評(píng)論
          圖片
          表情
          推薦
          點(diǎn)贊
          評(píng)論
          收藏
          分享

          手機(jī)掃一掃分享

          分享
          舉報(bào)
          <kbd id="afajh"><form id="afajh"></form></kbd>
          <strong id="afajh"><dl id="afajh"></dl></strong>
            <del id="afajh"><form id="afajh"></form></del>
                1. <th id="afajh"><progress id="afajh"></progress></th>
                  <b id="afajh"><abbr id="afajh"></abbr></b>
                  <th id="afajh"><progress id="afajh"></progress></th>
                  日韩香蕉视频 | 欧美黑吊大战白妞欧美大片 | 国产精品性爱视频 | xxxxx在线视频 | 天天综合7799 |