一、安裝Tesseract-OCR軟件
參考我的前一篇文章:Windows安裝Tesseract-OCR 4.00並配置環境變量
二、Python中使用
需要使用 pytesseract 庫,官方使用說明請看:https://pypi.python.org/pypi/pytesseract
1. 安裝依賴
1 pip install pytesseract 2 pip install pillow
2. 編寫代碼
准備識別下面這個驗證碼:
代碼如下:
1 import pytesseract 2 from PIL import Image 3 4 image = Image.open("code.png") 5 code = pytesseract.image_to_string(image) 6 print(code)
結果為6067,識別成功。
3. 如果出現錯誤,一般是系統變量設置的問題:
解決辦法一:根據安裝Tesseract軟件的步驟配置環境變量,設置好即可。
解決方法二:在代碼中添加相關變量參數:
1 import pytesseract 2 from PIL import Image 3 4 pytesseract.pytesseract.tesseract_cmd = 'D:/Program Files (x86)/Tesseract-OCR/tesseract.exe' 5 tessdata_dir_config = '--tessdata-dir "D:/Program Files (x86)/Tesseract-OCR/tessdata"' 6 7 image = Image.open("code.png") 8 code = pytesseract.image_to_string(image, config=tessdata_dir_config) 9 print(code)
--------------------------------------------------------------------------------------------------------------------
talk is cheap , show me the code.