今天這個問題困擾了我好久,開始直接用
pip install pytesseract
安裝了 pytesseract 然后出現了如下錯誤
Traceback (most recent call last): File "E:\eclipse_workspace\web_scraping\src\web_page_interaction\test.py", line 7, in <module> print pytesseract.image_to_string(image) # Run tesseract.exe on image File "F:\Python\lib\site-packages\pytesseract\pytesseract.py", line 161, in image_to_string config=config) File "F:\Python\lib\site-packages\pytesseract\pytesseract.py", line 94, in run_tesseract stderr=subprocess.PIPE) File "F:\Python\lib\subprocess.py", line 711, in __init__ errread, errwrite) File "F:\Python\lib\subprocess.py", line 959, in _execute_child startupinfo) WindowsError: [Error 2]
然后我去網上查找了各種方法無果,於是決定安裝 pytesser ,其安裝過程(windows)可以見:http://blog.csdn.net/evankaka/article/details/49533493
然后當安裝好后運行:
from PIL import Image from pytesser import * image = Image.open('captcha3.png') print image_to_string(image)
然后會出現一個 ImportError:這里的解決方案是跟進出現錯誤的文件: 將 import Image 改為 from PIL import Image
然后還是會出現最開始的錯誤:這里的話是因為 'F:\Python\Lib\site-packages\pytesser\pytesser.py' 里面的 tesseract_exe_name='tesseract' 里面是相對路徑,改成絕對路徑就好了:tesseract_exe_name='F:\\Python\\Lib\\site-packages\\pytesser\\tesseract'
最后就可以運行了:
運行結果:
最后,關於處理圖像的灰度等等問題可見: http://www.cnblogs.com/apexchu/p/4231041.html
這里我想問一個問題:為什么一張圖 img 要經過轉化成灰度圖后才能夠被識別?
t.png
t1.png
import pytesser from PIL import Image img = Image.open('t.png') print pytesser.image_to_string(img) img = img.convert('L') img.save('t1.png') print pytesser.image_to_string(img)
輸出結果為:
X
Ariali Amazingly few discotheques
provide jukeboxes.