本文實例講述了Python編程實現的圖片識別功能。分享給大家供大家參考,具體如下:
1. 安裝PIL,官方沒有WIN64位,Pillow替代
pip install Pillow-2.7.0-cp27-none-win_amd64.whl
2. 安裝Pytesser
下載pytesser_v0.0.1.zip,解壓后復制進Python27/Lib/site-packges/pytesser路徑下,無pytesser則新建
在Python27/Lib/site-packges/pytesser中新建一pytesser.pth文件,內容為pytesser
在pytesser內,修改三點
① pytesser.py修改成__init.py__
② 修改pytesser.py
import Image
改為
from PIL import Image
tesseract_exe_name = 'tesseract' 改為tesseract_exe_name = 'Python27//Lib//site-packges//pytesser//tesseract' 注意/轉義
③ 安裝Tesseract
下載Tesseract OCR engine:http://code.google.com/p/tesseract-ocr/ ,
下載后解壓,找到tessdata文件夾,用其替換掉pytesser解壓后的tessdata文件夾即可。
不過除了測試用驗證碼之外,其余的系統驗證碼的識別率很低。
附測試代碼
from pytesser import *from PIL import Image, ImageEnhanceim = Image.open('D:/Python27/Lib/site-packages/pytesser/phototest.tif')im2 = Image.open(r'D:/Python27/Lib/site-packages/pytesser/fnord.tif','r')im3 = Image.open(r'F:/PROJECT/python/code/Study_1/src/20170424/cp.jpg','r') #文件讀寫模式以防報錯#圖片處理1::黑白處理enhancer = ImageEnhance.Contrast(im3)image2 = enhancer.enhance(5)image2.show()print image_to_string(image2)#圖片處理2: 降噪處理imgry = im3.convert('L') #灰度處理#灰度處理基礎上二值化處理threshold = 140table = []for i in range(256): if i < threshold: table.append(0) else: table.append(1)out = imgry.point(table, '1')out.show()text = image_to_string(out)if text.isspace() : print "FAILE"else: print text#print text
希望本文所述對大家Python程序設計有所幫助。
新聞熱點
疑難解答