Python基于內(nèi)置庫pytesseract實現(xiàn)圖片驗證碼識別功能
這篇文章主要介紹了Python基于內(nèi)置庫pytesseract實現(xiàn)圖片驗證碼識別功能,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
環(huán)境準(zhǔn)備:
1、安裝Tesseract模塊
git文檔地址:https://digi.bib.uni-mannheim.de/tesseract/
下載后就是一個exe安裝包,直接右擊安裝即可,安裝完成之后,配置一下環(huán)境變量,編輯 系統(tǒng)變量里面 path,添加下面的安裝路徑:
2、如果您想使用其他語言,請下載相應(yīng)的數(shù)據(jù),(我們只做中文,暫時下載一個中文的文字訓(xùn)練數(shù)據(jù)就可以) ,然后將.traineddata文件復(fù)制到'tessdata'目錄中。C:\Program Files (x86)\Tesseract-OCR\tessdata
3、配置環(huán)境變量:
編輯 系統(tǒng)變量里面 path,添加下面的安裝路徑:C:\Program Files (x86)\Tesseract-OCR
cmd命令模式下測試是否安裝成功:
tesseract test.jpg text -l chi_sim
4、安裝python的第三方庫:
pip install pillow #一個python的圖像處理庫,pytesseract依賴 pip install pytesseract
5、找到pytesseract的安裝包,C:\Python34\Lib\site-packages\pytesseract,編輯pytesseract.py文件(此步驟必須做,否則運行代碼時會報錯):
tesseract_cmd = 'C:/Program Files (x86)/Tesseract-OCR/tesseract.exe'
代碼實例:
簡單驗證碼代碼:
import requests from PIL import Image import pytesseract ''' 簡單驗證碼 ''' # 驗證碼地址 url = "https://www.renrendai.com/passport/index/captcha?time=1551682134111" response = requests.get(url).content #將圖片寫入文件 with open('yzm.png','wb') as f: f.write(response) f.close() '''識別驗證碼''' #第一步:通過內(nèi)置模塊PIL打開文件 pic = Image.open('yzm.png') #第二步:識別圖片中的內(nèi)容 pic_str = pytesseract.image_to_string(pic) print("驗證碼識別結(jié)果為:",pic_str)
百度文庫圖片文檔的識別:
#下載圖片 baidu_url = "https://wkretype.bdimg.com/retype/zoom/4127ed79a26925c52cc5bf99?pn=2&o=jpg_6&md5sum=9cdc209bc34a40ed774f7e14c0be59c4&sign=5dbcb28bf1&png=11238-22475&jpg=41808-117940" baidu_pic = requests.get(baidu_url).content #圖片寫入文件 with open('baidu_pic.jpg','wb') as f: f.write(baidu_pic) f.close() #識別驗證碼 baidu_img = Image.open('baidu_pic.jpg') baidu_img_str = pytesseract.image_to_string(baidu_img,lang="chi_sim") print('百度文庫圖片內(nèi)容為:',baidu_img_str)
復(fù)雜的驗證碼,直接識別不了,可以使用超級鷹的第三方接口,如有需要,自己進行賬號的注冊,這里直接貼代碼嘍:
from chaojiying import Chaojiying chaojiying_url= "http://www.chaojiying.com/include/code/code.php?u=1" response = requests.get(chaojiying_url).content with open('rryz.png','wb') as f: f.write(response) f.close() #讀取文件內(nèi)容 with open('rryz.png','rb') as f: pic1 = f.read() #調(diào)用第三方打碼平臺接口識別驗證碼 yz = Chaojiying(username='*****', password='****', soft_id='****') res = yz.post_pic(pic1,codetype='1902').get('pic_str') #1902 驗證碼類型 print('識別的結(jié)果:',res)
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
使用python-pptx包批量修改ppt格式的實現(xiàn)
今天小編就為大家分享一篇使用python-pptx包批量修改ppt格式的實現(xiàn),具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-02-02pandas series序列轉(zhuǎn)化為星期幾的實例
下面小編就為大家分享一篇pandas series序列轉(zhuǎn)化為星期幾的實例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-04-04