UnicodeDecodeError with Tesseract OCR in Python
The problem is that python is trying to use the console's encoding (CP1252) instead of what it's meant to use (UTF-8). PyTesseract has found a unicode character and is now trying to translate it into CP1252, which it can't do. On another platform you won't encounter this error because it will get to use UTF-8.
You can try using a different function (possibly one that returns bytes
instead of str
so you won't have to worry about encoding). You could change the default encoding of python as mentioned in one of the comments, although that will cause problems when you go to try and print the string on the windows console. Or, and this is my recommended solution, you could download Cygwin and run python on that to get a clean UTF-8 output.
If you want a quick and dirty solution that won't break anything (yet), here's a way that you might consider:
import builtins
original_open = open
def bin_open(filename, mode='rb'): # note, the default mode now opens in binary
return original_open(filename, mode)
from PIL import Image
import pytesseract
img = Image.open('binarized_image.png')
try:
builtins.open = bin_open
bts = pytesseract.image_to_string(img)
finally:
builtins.open = original_open
print(str(bts, 'cp1252', 'ignore'))
I've had the same problem as you but I had to save the output of pytesseract to a file. So, I created a function for ocr with pytesseract and when saving to a file added parameter encoding='utf-8'
so my function now looks like this:
def image_ocr(image_path, output_txt_file_name):
image_text = pytesseract.image_to_string(image_path, lang='eng+ces', config='--psm 1')
with open(output_txt_file_name, 'w+', encoding='utf-8') as f:
f.write(image_text)
I hope this helps someone :)