In this tutorial we will explore how to extract text from image using Python.

Table of Contents



Introduction

Extracting text from images is a very popular task in the operations units of the business (extracting information from invoices and receipts) as well as in other areas.

OCR (Optical Character Recognition) is an electronic computer-based approach to convert images of text into machine-encoded text, which can then be extracted and used in text format.

To continue following this tutorial we will need:

Tesseract is an open source OCR (optical character recognition) engine which allows to extract text from images.

In order to use it in Python, we will also need the pytesseract library which is a wrapper for Tesseract engine.

Since we are working with images, we will also need the pillow library which adds image processing capabilities to Python.

First, search for the Tesseract installer for your operating system. For Windows, you can find the latest version of Tesseract installer here. Simply download the .exe file and install on your computer.

If you don’t have the Python libraries installed, please open “Command Prompt” (on Windows) and install them using the following code:


pip install pytesseract
pip install pillow

Sample images

In order to continue in this tutorial we will need some images to work with.

Here are the three images we will use in this tutorial:

sample text for OCR

sample text for OCR

sample text for OCR

In this tutorial we will use simple images with text aligned horizontally that don’t require any additional image processing.


Extract text from a single image using Python

Let’s start with extracting text from a single image using Python.

For this example, we will work with the first image provided in the previous section: sampletext1-ocr.png

Here is how the structure of my files look like:

All images are placed in the folder images and the code resides in main.py

The path to the image we need is: images/sampletext1-ocr.png

Another path we need is the path to the tessaract.exe which was created after the installation. On Windows it should reside in: C:\Program Files\Tesseract-OCR\tesseract.exe

Now we have everything we need and can easily extract text from image using Python:


from PIL import Image
from pytesseract import pytesseract

#Define path to tessaract.exe
path_to_tesseract = r'C:\Program Files\Tesseract-OCR\tesseract.exe'

#Define path to image
path_to_image = 'images/sampletext1-ocr.png'

#Point tessaract_cmd to tessaract.exe
pytesseract.tesseract_cmd = path_to_tesseract

#Open image with PIL
img = Image.open(path_to_image)

#Extract text from image
text = pytesseract.image_to_string(img)

print(text)

And you should get:

Sample Text 1

Extract text from multiple images using Python

In this section we will explore how to extract text from multiple images using Python.

We know that all images are placed in the folder images and the code resides in main.py

One way of extracting text from every image would be to use the file names of every image and extract text from those images one by one.

But what if we have 100 images in the folder? Using the os library we can access all of the file names in a given directory.

Once we get access to all of the file names in the images folder, we will iterate over them and extract text from each image using Python:


from PIL import Image
from pytesseract import pytesseract
import os

#Define path to tessaract.exe
path_to_tesseract = r'C:\Program Files\Tesseract-OCR\tesseract.exe'

#Define path to images folder
path_to_images = r'images/'

#Point tessaract_cmd to tessaract.exe
pytesseract.tesseract_cmd = path_to_tesseract

#Get the file names in the directory
for root, dirs, file_names in os.walk(path_to_images):
    #Iterate over each file name in the folder
    for file_name in file_names:
        #Open image with PIL
        img = Image.open(path_to_images + file_name)

        #Extract text from image
        text = pytesseract.image_to_string(img)

        print(text)

And you should get:

Sample Text 1
Sample Text 2
Sample Text 3

which is exactly the text we have in the images.


Conclusion

In this article we explored how to extract text from a single image and multiple images using Python and Tesseract.

Feel free to leave comments below if you have any questions or have suggestions for some edits and check out more of my Python Programming tutorials.