2017-04-13 19 views
2

我面臨着一個令人困惑的問題,嘗試下載圖像並打開它與BytesIO,以從中提取文本使用PIL & pytesseract。從URL打開圖像文件與PIL用於pytesseract文本識別

>>> response = requests.get('http://abc/images/im.jpg') 
>>> img = Image.open(BytesIO(response.content)) 
>>> img 
<PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=217x16 at 0x7FDAD185CB38> 
>>> text = pytesseract.image_to_string(img) 
>>> text 
'' 

這裏給出一個空字符串。

但是,如果我保存圖像,然後用pytesseract再次打開它,它會給出正確的結果。

>>> img.save('im1.jpg') 
>>> im = Image.open('im1.jpg') 
>>> pytesseract.image_to_string(im) 
'The right text' 

而只是爲了確認,兩者給出相同的大小。

>>> im.size 
(217, 16) 
>>> img.size 
(217, 16) 

可能是什麼問題?是否有必要保存圖像或我做錯了什麼?

+0

什麼一些反饋的答案我有provi這些信息呢? – Claudio

回答

0

您似乎有一個我無法複製的問題。所以要診斷你的問題,如果有的話,有更多的細節是必要的,但不要問我細節,我只是假設(所以我的整體經驗),在提供細節的過程中,你的問題將消失,無法複製。這種方式是這個答案解決您的問題。

如果不是,請告知是否需要進一步幫助。至少你可以肯定,你一般都是正確的,因爲你曾經經歷過的事情,並沒有做出任何明顯錯誤的事情。

這裏的完整代碼(你的問題是缺少提示哪些模塊是必要的)和圖像實際上是在線,所以任何人都可以測試代碼是否工作(你沒有提供一個在線現有圖像在你的問題):

import io 
import requests 
import pytesseract 
from PIL import Image 
response = requests.get("http://www.teamjimmyjoe.com/wp-content/uploads/2014/09/Classic-Best-Funny-Text-Messages-earthquake-titties.jpg") 
# print(type(response)) # <class 'requests.models.Response'> 
img = Image.open(io.BytesIO(response.content)) 
# print(type(img)) # <class 'PIL.JpegImagePlugin.JpegImageFile'> 
text = pytesseract.image_to_string(img) 
print(text) 

這裏pytesseract輸出:

Hey! I just saw on CNN 
there was an earthquake 
near you. Are you ok? 






‘ Yes! We‘re all line! 

What did it rate on the titty 
scale? 
‘ Well they only jiggled a 

little bit, so probably not 

that high. 
HAHAHAHAHAHA I LOVE 
YOU 
Richter scale. My phone is l 
a 12 yr old boy. 

我的系統:Linux Mint的18.1與Python 3.6

相關問題