【问题标题】:Opening Image file from url with PIL for text recognition with pytesseract使用 PIL 从 url 打开图像文件以使用 pytesseract 进行文本识别
【发布时间】:2017-09-10 05:09:06
【问题描述】:

我在尝试下载图像并使用 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
''

这里给出一个空字符串。

但是,如果我保存图像然后用 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)

可能是什么问题?是需要保存图片还是我做错了什么?

【问题讨论】:

  • 对我提供的答案有什么反馈?

标签: image python-3.x request python-imaging-library ocr


【解决方案1】:

您似乎遇到了我无法重现的问题。因此,要诊断您的问题(如果有的话),是否需要更多细节,但不是询问细节,我只是假设(所以我的整体经验)在提供细节的过程中,您的问题将消失并且无法重现.这种方式就是这个答案解决您的问题。

如果不是,请告知您是否需要进一步的帮助。至少你可以肯定,你通常是对的,因为你所经历的并且没有做任何明显错误的事情。

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

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

【讨论】:

    【解决方案2】:

    尝试在调用pytesseract之前添加tesseract的路径

    from PIL import Image
    from pytesseract import pytesseract
    import requests
    from io import BytesIO
    def ImageToText(ImageURl):
        response = requests.get(ImageURl)
        #add path to tesseract
        path_to_tesseract = r"C:\Program Files\Tesseract-OCR\tesseract.exe"
        img = Image.open(BytesIO(response.content))
        pytesseract.tesseract_cmd = path_to_tesseract
        text = pytesseract.image_to_string(img)
        return text
    

    【讨论】:

    • 您的答案可以通过额外的支持信息得到改进。请edit 添加更多详细信息,例如引用或文档,以便其他人可以确认您的答案是正确的。你可以找到更多关于如何写好答案的信息in the help center
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-10-11
    • 1970-01-01
    • 2019-12-04
    • 2020-06-22
    • 1970-01-01
    • 2020-10-17
    相关资源
    最近更新 更多