当前位置: 代码迷 >> python >> 从图像中读取文本时出现 Unicode 解码错误
  详细解决方案

从图像中读取文本时出现 Unicode 解码错误

热度:51   发布时间:2023-07-16 10:41:58.0

我使用此代码从图像文件中读取文本。

代码如下

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)

错误:UnicodeDecodeError:“charmap”编解码器无法解码位置 278 中的字节 0x81:字符映射到

尝试通过以下方式解决此错误:

然后得到错误:

UnicodeDecodeError: 'utf-8' 编解码器无法解码位置 0 中的字节 0xff:起始字节无效

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

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

以二进制格式加载图像。

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

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

希望它有帮助!

  相关解决方案