python3:識別圖片文字

1.安裝Python依賴庫:pytesseract 和 PILpython

# 安裝pytesseract
pip install pytesseract

# 安裝PIL
pip install PIL
#此處若是電腦是window64位系統不能直接使用pip安裝,解決方法:
1.先下載:https://www.lfd.uci.edu/~gohlke/pythonlibs/ 根據Python版本下載對應的版本,個人是python3.5因此下載:
 Pillow-5.0.0-cp35-cp35m-win_amd64.whl
2.安裝wheel依賴,由於安裝Pillow-5.0.0-cp35-cp35m-win_amd64.whl須要wheel依賴
pip install wheel
3.切換到Pillow-5.0.0-cp35-cp35m-win_amd64.whl的下載目錄,執行安裝名稱
pip intall Pillow-5.0.0-cp35-cp35m-win_amd64.whl
到此PIL安裝完畢

# 安裝識別引擎tesseract-ocr
1.下載安裝包tesseract-ocr安裝包和中文語言包
下載好後直接點擊.exe文件安裝便可,默認不支持中文,若是想要支持英文須要把中文包複製到tesseract-ocr/tessdata的目錄下

到此就能夠使用圖片識別文字了,可是隻是支持英文測試

測試圖片(test2.png):code

from PIL import Image
import pytesseract

#測試的圖片資源
text = pytesseract.image_to_string(Image.open('test2.png'))
print(text)

結果:圖片

若是須要識別中文須要配置庫路徑ip

打開python安裝目錄,找到並編輯D:\python\Lib\site-packages\pytesseract\pytesseract.pyci

#tesseract_cmd = 'tesseract'
tesseract_cmd = 'C:/Program Files (x86)/Tesseract-OCR/tesseract.exe'資源

保存後就能夠支持中文識別了cmd

測試圖片:string

結果:it

能夠看出其實中文識別性仍是兒童級別的!

相關文章
相關標籤/搜索