【发布时间】: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