Python3使用 pytesseract 進行圖片識別

1、安裝Tesseract-OCR軟件

參考個人前一篇文章:Windows安裝Tesseract-OCR 4.00並配置環境變量python

2、Python中使用

須要使用 pytesseract 庫,官方使用說明請看:https://pypi.python.org/pypi/...segmentfault

1. 安裝依賴

pip install pytesseract
pip install pillow

2. 編寫代碼

準備識別下面這個驗證碼:
clipboard.pngspa

代碼以下code

import pytesseract
from PIL import Image

image = Image.open("code.png")
code = pytesseract.image_to_string(image)
print(code)

clipboard.png
結果爲6067,識別成功。ip

3. 若是出現錯誤,通常是系統變量設置的問題:

解決辦法一:根據安裝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)

clipboard.png

相關文章
相關標籤/搜索