0
votes

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

Tried to solve this error from following: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

2
At what point are you getting this error? On the Image.open()? the image_to_string() ?? - Kingsley

2 Answers

0
votes

As per Image documentation (help(Image.open)), the image files must be opened in the binary mode:

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

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 !