0

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

DYZ
  • 55,249
  • 10
  • 64
  • 93
Robin
  • 39
  • 1
  • 7

2 Answers2

0

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

open('maxresdefault.jpg', 'rb')
DYZ
  • 55,249
  • 10
  • 64
  • 93
0

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 !

Ebin Zacharias
  • 195
  • 1
  • 7