使用 PIL 打开来自 url 的图像文件以使用 pytesseract 进行文本识别
Opening Image file from url with PIL for text recognition with pytesseract
我在尝试下载图像并使用 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)
可能是什么问题?是需要保存图片还是我做错了什么?
您似乎遇到了一个我无法重现的问题。所以要诊断你的问题,如果有的话,需要更多的细节,但我只是假设(所以我的整体经验)而不是询问细节,在提供细节的过程中你的问题将消失并且无法重现.这种方式就是解决您的问题的方法。
如果不是,请告知您是否需要进一步的帮助。至少你可以确定,由于你的经历,你通常是对的,并且没有做任何明显错误的事情。
这里是完整代码(您的问题缺少哪些模块是必需的提示)并且图像实际上是在线的,因此其他任何人也可以测试代码是否有效(您没有在您的网站中提供在线现有图像问题):
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
尝试在调用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
我在尝试下载图像并使用 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)
可能是什么问题?是需要保存图片还是我做错了什么?
您似乎遇到了一个我无法重现的问题。所以要诊断你的问题,如果有的话,需要更多的细节,但我只是假设(所以我的整体经验)而不是询问细节,在提供细节的过程中你的问题将消失并且无法重现.这种方式就是解决您的问题的方法。
如果不是,请告知您是否需要进一步的帮助。至少你可以确定,由于你的经历,你通常是对的,并且没有做任何明显错误的事情。
这里是完整代码(您的问题缺少哪些模块是必需的提示)并且图像实际上是在线的,因此其他任何人也可以测试代码是否有效(您没有在您的网站中提供在线现有图像问题):
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
尝试在调用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