Skip to content

OCR in Python is very easy


Yes … I’ve surprised for how easy is to deal with Optical Character Recognition OCR using Python 2.x, …. if you have the right tools installed.

I decided to try OCR because I received a WhatsApp message with a photo of the monthly menu at school, and … why not can I study what the children are eating?

If this post helps you, buy me a beer!

Installing the tools

I did a Google search for that, and … the road to do it doesn’t seem very difficult. The instructions followed were found here: The first step is install PIL, a package that allows to deal with images. If you are using the Anaconda distribution, you don’t need to install anything, because it’s already done for you!


PIL is intalled in Anaconda Python distribution

As you can see, the import gives no error (in case you need to install PIL, here you can read how to install it). Next, you need to install tesseract-ocr.

Instalando Tesser

Install Tesser

Click next, and in one step, some elements need to be downloaded:

Almost done:


Installing tesser

Next step is to download the PyTesser library (, the tool that allows Python to work with Tesser. Unzip the file on the same dir where you have your code.

Trying OCR with one image

Let’s code some lines, but before going on, you only need the image, and run this little code:

from PIL import Image
from pytesser import *

image_file = 'menu.tif'
im =
text = image_to_string(im)
text = image_file_to_string(image_file)
text = image_file_to_string(image_file, graceful_errors=True)
print "=====output=======\n"
print text

In my case, menu.tif is the image I want to try.

ejemplo para PIL

menuejemplo for OCR using PIL

As you can see in the next image, the outcome is not perfect (as I expected):

Prueba OCR en Python

Testing OCR with Python

The code and the tool works, but … it works better if the image has better quality.

Trying an example

Let’s verify the OCR works better with a prepared image, and the best option id to try the image included on PyTesser. The image is this:

Probando OCR y PyTesser

Testing OCR with PyTesser

And the outcome:

Prueba PyTesser

Using PyTesser

And … that’s all. If you like it, I encaurage you donate for keep on doing this blog!!

By now, I wasn’t able to analize the menu because of the data extraction method is not perfect. In order to improve the data extraction, I manage the following scenarios: better photos, or another method. The first is the easiest method.

Have a nice day, and keep coding!

Manejando Datos Newsletter

Noticias, entradas, eventos, cursos, … News, entrances, events, courses, …

Gracias por unirte a la newsletter. Thanks for joining the newsletter.