从imag读取文本时出现Unicode解码错误

2024-04-25 03:40:41 发布

您现在位置:Python中文网/ 问答频道 /正文

我用这个代码从图像文件中读取文本。 Reading text from image

代码如下

from PIL import Image
from pytesseract import image_to_string

image = Image.open("image.jpg",'r')

myText = image_to_string(Image.open(open('maxresdefault.jpg')),config='-psm 10')
myText = image_to_string(Image.open(open('maxresdefault.jpg')))
print(myText)

Error: UnicodeDecodeError: 'charmap' codec can't decode byte 0x81 in position 278: character maps to

试图从以下位置解决此错误:UnicodeDecodeError: 'charmap' codec can't decode byte X in position Y: character maps to <undefined>

然后出现错误:

UnicodeDecodeError: 'utf-8' codec can't decode byte 0xff in position 0: invalid start byte


Tags: toinfromimagestringpositionopenbyte
2条回答

根据Image文档(help(Image.open)),必须以二进制模式打开图像文件:

open('maxresdefault.jpg', 'rb')

以二进制格式加载图像。在

更改以下代码为我解决了这个问题。在

import PIL.Image
pil_image = PIL.Image.open(image_path, "rb")

希望有帮助!在

相关问题 更多 >