本文實例講述了Python實現識別圖片內容的方法。分享給大家供大家參考,具體如下:
python識別圖片內容。
這里我的環境為windows64位,python2.7.14
需要用到PIL模塊和tesseract模塊。
首先需要安裝pip包管理,安裝方法可參考附錄windows下安裝python包管理器pip
安裝PIL模塊:
pip install Pillow
tesseract模塊安裝:
pip install pytesseract
安裝識別引擎和中文語言包,點擊此處本站下載。
下載完成解壓:
1.雙擊tesseract-ocr-setup-3.02.02.exe安裝,安裝完成后,需要指定tesseract模塊識別引擎的程序路徑,打開python安裝路徑的模塊路徑,我的路徑為:E:\wamp\python\Lib\site-packages\,進入pytesseract,編輯pytesseract.py文件,修改 tesseract_cmd = 'tesseract' 的內容:
# tesseract_cmd = 'tesseract' # 修改為安裝Tesseract-OCR的真實路徑
tesseract_cmd = 'E:/wamp/python modules/Tesseract-OCR/tesseract.exe'
2.進入解壓包中的中文語言包,復制chi_sim.traineddata到Tesseract-OCR安裝目錄下tessdata文件夾下,我的路徑:E:\wamp\python modules\Tesseract-OCR\tessdata\chi_sim.traineddata
環境配置完成。
python代碼:
# -*- coding: UTF-8 -*-
from PIL import Image
import pytesseract
# 識別中文
text = pytesseract.image_to_string(Image.open('chinese.png'),lang='chi_sim')
print text
# 識別英文
text = pytesseract.image_to_string(Image.open('english.png'))
print text
附:windows下安裝python包管理器pip
windows下安裝python包管理器pip。
pip下載地址:https://pypi.python.org/pypi/pip#downloads
選擇 pip-9.0.1.tar.gz (md5, pgp)
下載完成解壓,這里我解壓到D:\python\pip-9.0.1,命令行進入D:\python\pip-9.0.1
cd D:\python\pip-9.0.1
安裝pip
python setup.py install
安裝完成,輸入pip list查看是否成功
pip list
顯示下面信息代表安裝成功:
pip (9.0.1)
setuptools (28.8.0)
pip安裝模塊命令:
pip install xxx
更多關于Python相關內容可查看本站專題:《Python數學運算技巧總結》、《Python圖片操作技巧總結》、《Python數據結構與算法教程》、《Python函數使用技巧總結》、《Python字符串操作技巧匯總》及《Python入門與進階經典教程》
希望本文所述對大家Python程序設計有所幫助。