繁体   English   中英

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

[英]Unicode Decode Error while 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)

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

尝试通过以下方式解决此错误: UnicodeDecodeError: 'charmap' codec can't decode byte X in position Y: character maps to <undefined>

然后得到错误:

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

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

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

以二进制格式加载图像。

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

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

希望它有帮助!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM