Python3使用 pytesseract 進行圖片識別


一、安裝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.

 

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM