[英]Unicode Decode Error while reading text from image
I have used this code to read text from an image file.我使用此代码从图像文件中读取文本。 Reading text from image
从图像中读取文本
The code is as follows代码如下
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”编解码器无法解码位置 278 中的字节 0x81:字符映射到
Tried to solve this error from following: UnicodeDecodeError: 'charmap' codec can't decode byte X in position Y: character maps to <undefined>尝试通过以下方式解决此错误: UnicodeDecodeError: 'charmap' codec can't decode byte X in position Y: character maps to <undefined>
Then got error:然后得到错误:
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xff in position 0: invalid start byte
UnicodeDecodeError: 'utf-8' 编解码器无法解码位置 0 中的字节 0xff:起始字节无效
根据Image
文档( help(Image.open)
),图像文件必须以二进制模式打开:
open('maxresdefault.jpg', 'rb')
Load the Image in binary format.以二进制格式加载图像。
Changing the following code solved the problem for me.更改以下代码为我解决了问题。
import PIL.Image
pil_image = PIL.Image.open(image_path, "rb")
Hope it helps !希望它有帮助!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.