如何将图像从url转换成文本,使用pytesseract而不保存在内存中。

问题描述 投票:1回答:1

我有一个包含图像的URL,其中只有文本。我想提取图片中的文字。我可以使用pytesseract找到一个解决方案。但是,我需要将图像保存在本地内存中& 然后在函数中使用它来获取文本。有什么方法可以在内存中实现这个功能吗?

img_resp = requests.get(img_url)
with open('test.png','wb') as img:
    img.write(img_resp.content)
print(image_to_string(Image.open('test.png')))
python python-3.x python-imaging-library python-tesseract
1个回答
1
投票

你可以将响应内容传递给 Image.open 像这样。

import io
import requests
from PIL import Image

img_resp = requests.get(img_url)
img = Image.open(io.BytesIO(img_resp.content))
© www.soinside.com 2019 - 2024. All rights reserved.