How do i convert an image to text in python?

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

How do i convert an image to text in python?

Photo by Ian Schneider on Unsplash

Table of Contents

  • Introduction
  • Sample images
  • Extract text from a single image using Python
  • Extract text from multiple images using Python
  • Conclusion

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
  • pytesseract
  • pillow

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:

How do i convert an image to text in python?

Image by Author

How do i convert an image to text in python?

Image by Author

How do i convert an image to text in python?

Image by Author

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:

How do i convert an image to text in python?

Image by Author

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:

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:

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.

Can we convert image to text using Python?

Streamline Your Images into Text with Python and OCR.
OCR stands for Object Character Recognition !!!!.
Step 1: Install tesseract using tessdoc as per the OS availability..
Step 2: Check and confirm the installation path for tesseract package..
Step 3: Install pytesseract using pip..

How do I convert an image to a string in Python?

How to convert an image to a string in Python.
an_image = PIL. Image. open("an_image.png").
output = io. BytesIO().
an_image. save(output, format="png").
image_as_string = output. getvalue().
print(image_as_string [:20]).

How can I convert an image into text?

Convert an image file.
On your computer, go to drive.google.com..
Right-click on the desired file..
Click Open with. Google Docs..
The image file will be converted to a Google Doc, but some formatting might not transfer: Bold, italics, font size, font type, and line breaks are most likely to be retained..

How do I extract text from an image using Python?

Extract text from a single image using Python.
from PIL import Image..
from pytesseract import pytesseract..
img = Image. open(path_to_image).
text = pytesseract. image_to_string(img).
print(text).