使用PIL从URL打开图像文件以使用pytesseract进行文本识别

spr*_*ksh 3 ocr image request python-imaging-library python-3.x

我试图下载图像并使用BytesIO打开图像以使用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
''
Run Code Online (Sandbox Code Playgroud)

这里给出一个空字符串。

但是,如果我保存图像,然后用pytesseract再次打开它,它将给出正确的结果。

>>> img.save('im1.jpg')
>>> im = Image.open('im1.jpg')
>>> pytesseract.image_to_string(im)
'The right text'
Run Code Online (Sandbox Code Playgroud)

只是为了确认,两者的大小相同。

>>> im.size
(217, 16)
>>> img.size
(217, 16)
Run Code Online (Sandbox Code Playgroud)

可能是什么问题?是否需要保存图像或者我做错了什么?

Cla*_*dio 7

您似乎有一个我无法重现的问题。因此,要诊断您的问题(如果有),还需要更多详细信息,但我只是要求(而不是要求提供详细信息)我只是假设(因此,根据我的整体经验),在提供详细信息的过程中您的问题将消失并且无法重现。这样可以为您的问题提供解决方案。

如果不是,请告知是否需要进一步的帮助。至少您可以确定,由于经历过的事情并且没有做任何明显错误的事情,因此您通常是正确的。

这里是完整代码(您的问题缺少提示,提示需要哪些模块)并且该图像实际上是在线的,因此其他任何人也可以测试该代码是否有效(您未在问题中提供在线现有图像):

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 )
Run Code Online (Sandbox Code Playgroud)

这是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.
Run Code Online (Sandbox Code Playgroud)

我的系统:带有Python 3.6的Linux Mint 18.1