1.安裝Pillowpython
pip install Pillow
2.安裝tesseract-ocrgit
github地址: https://github.com/tesseract-ocr/tesseractgithub
You can either Install Tesseract via pre-built binary package or build it from source.ubuntu
windows:windows
The latest installer can be downloaded here: tesseract-ocr-setup-3.05.01.exe and tesseract-ocr-setup-4.00.00dev.exe (experimental). ide
ubuntu:ui
sudo apt-get install tesseract-ocr
traineddata文件路徑: /usr/share/tesseract-ocr/tessdata/code
3.安裝pytesseractblog
pip install pytesseract
遇到的問題:ip
1.FileNotFoundError: [WinError 2] 系統找不到指定的文件
解決方法:
方法1[推薦]: 將tesseract.exe添加到環境變量PATH中,
例如: D:\Tesseract-OCR,默認路徑爲C:\Program Files (x86)\Tesseract-OCR
注意: 爲了使環境變量生效,須要關閉cmd窗口或是關閉pycharm等ide從新啓動
方法2: 修改pytesseract.py文件,指定tesseract.exe安裝路徑
# CHANGE THIS IF TESSERACT IS NOT IN YOUR PATH, OR IS NAMED DIFFERENTLY tesseract_cmd = 'C:\\Program Files (x86)\\Tesseract-OCR\\tesseract.exe‘
方法3: 在實際運行代碼中指定
pytesseract.pytesseract.tesseract_cmd = 'D:\\Tesseract-OCR\\tesseract.exe'
2.pytesseract.pytesseract.TesseractError: (1, 'Error opening data file \\Tesseract-OCR\\tessdata/eng.traineddata')
解決方法:
方法1[推薦]:
將tessdata目錄的上級目錄所在路徑(默認爲tesseract-ocr安裝目錄)添加至TESSDATA_PREFIX環境變量中
例如: C:\Program Files (x86)\Tesseract-OCR
Please make sure the TESSDATA_PREFIX environment variable is set to the parent directory of your "tessdata" directory.
方法2: 在.py文件配置中指定tessdata-dir
tessdata_dir_config = '--tessdata-dir "D:\\Tesseract-OCR\\tessdata"' # tessdata_dir_config = '--tessdata-dir "'C:\\Program Files (x86)\\Tesseract-OCR\\tessdata"' pytesseract.image_to_string(image, config=tessdata_dir_config)
附: trainedata下載地址: the latest from github.com
參考文檔: