Python中pytesseract庫的使用以及注意事項


當我們在使用pytesseract庫的時候,使用 pip install pytesseract安裝完成后,發現它並不能識別出圖片內容,並且會拋出異常pytesseract.pytesseract.TesseractNotFoundError: tesseract is not installed or it's not in your PATH. See README file for more information.
這是怎么回事呢?今天讓我們一探究竟

嘗試

使用代碼

import pytesseract
from PIL import Image

image = Image.open("./NormalImg.png")
text = pytesseract.image_to_string(image)
print(text)

報錯提示:

    raise TesseractNotFoundError()
pytesseract.pytesseract.TesseractNotFoundError: tesseract is not installed or it's not in your PATH. See README file for more information.

官方文檔

pytesseract官方文檔:https://pypi.org/project/pytesseract/

是我們缺少了tesseract程序

tesseract官方Github地址:https://github.com/UB-Mannheim/tesseract

tesseract官方Github說明https://github.com/UB-Mannheim/tesseract/wiki

安裝tesseract

下載地址

Tesseract 5.0.0 32位版本:tesseract-ocr-w32-setup-v5.0.0-alpha.20200328.exe (32 bit)

Tesseract 5.0.0 64位版本:tesseract-ocr-w64-setup-v5.0.0-alpha.20200328.exe (64 bit)

新增百度雲盤鏈接:
鏈接:https://pan.baidu.com/s/1EO5tFmzn1hqY_M679eSBnw
提取碼:nyw4

導入tesseract.exe執行文件地址

添加以下導入路徑:

pytesseract.pytesseract.tesseract_cmd = r'C:\Program Files\Tesseract-OCR\tesseract.exe'

最終代碼:

import pytesseract
from PIL import Image

pytesseract.pytesseract.tesseract_cmd = r'C:\Program Files\Tesseract-OCR\tesseract.exe'
image = Image.open("./NormalImg.png")
text = pytesseract.image_to_string(image)
print(text)

至此運行代碼不會異常,並可以正常讀取圖片文字內容

總結

pytesseract包依賴於Tesseract執行文件,需要安裝Tesseract

當然Tesseract只能識別標准的ASCII字符串,復雜的驗證嗎就無法使用pytesseract來讀取了


歡迎來跟博主討論Python有關的問題。


免責聲明!

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



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