參考個人前一篇文章:Windows安裝Tesseract-OCR 4.00並配置環境變量python
須要使用 pytesseract 庫,官方使用說明請看:https://pypi.python.org/pypi/...segmentfault
pip install pytesseract pip install pillow
準備識別下面這個驗證碼:spa
代碼以下code
import pytesseract from PIL import Image image = Image.open("code.png") code = pytesseract.image_to_string(image) print(code)
結果爲6067,識別成功。ip
解決辦法一:根據安裝Tesseract軟件的步驟配置環境變量,設置好便可。
解決方法二:在代碼中添加相關變量參數:get
import pytesseract from PIL import Image pytesseract.pytesseract.tesseract_cmd = 'D:/Program Files (x86)/Tesseract-OCR/tesseract.exe' tessdata_dir_config = '--tessdata-dir "D:/Program Files (x86)/Tesseract-OCR/tessdata"' image = Image.open("code.png") code = pytesseract.image_to_string(image, config=tessdata_dir_config) print(code)