我有一个包含图像的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')))
你可以将响应内容传递给 Image.open
像这样。
import io
import requests
from PIL import Image
img_resp = requests.get(img_url)
img = Image.open(io.BytesIO(img_resp.content))