Extract Texts From Image Using OpenCV In Python

opencv in python image

Have you ever wondered how you can extract texts from multiple pictures or screenshots without going through the boring process of reading and typing them?

As always Python makes boring stuff super fun! I have initially written a post on how you can extract texts from an image using pytesseract.

However, on this post we are going to do same thing in a bit advanced manner using OpenCV in python. OpenCV is a very useful and powerful python modules used in complex projects like machine learning, AI and similar projects.

I won’t waste your time on the history of OpenCV, you can click here to explore the module further. Without further ado, let’s start coding things up.

WHAT YOU WILL LEARN

  • How to combine OpenCV and Pytesseract to write a program that will extract texts from all the images in a given directory/folder.
  • How to write a re-usable function that will do the above-mentioned operation.

MY ASSUMPTIONS ABOUT YOU

  • That you are already familiar with basic python concepts like strings, f-strings, for loops and importing of modules
  • That you are already familiar with os module and pytesseract, you can check this tutorial for pytesseract.

LET’S CODE A TEXT EXTRACTOR USING OPENCV IN PYTHON

First off, we will start by importing the relevant modules.

import pytesseract as pt
import os
import cv2

You should make sure you have opencv and pytesseract installed on your machine before importing. You may use pip to install the module [pip install opencv-python].

For pytesseract check this tutorial again to get up to speed. We will now create our image path variable and also create an empty string to catch all the texts from all the images.

path = '/home/you/Documents/screenshot_folder/'
#let's check and confirm the images in our chosen directory/path
print(os.listdir(path))

#let's create an empty list for the texts we will extract
extracted_text_list = ''

The code above will print all the names of images you have on the image folder you want to use. Your path name/pattern might look different depending on your operating system.

The extracted_text_list variable will catch all the texts when we start extraction.

In case this is your first time coming across OpenCv in python, you might want to explore the module further after this tutorial.

Now let’s write our main program and put it all together in one code:

import pytesseract as pt
import os
import cv2

path = '/home/you/Documents/screenshot_folder/'
#let's check and confirm the images in our chosen directory/path
print(os.listdir(path))

#let's create an empty list for the texts we will extract
extracted_text_list = ''

#let's write a code to extract and add the texts to our extracted_text_list
for image in os.listdir(path):
    img_obj = cv2.imread(f'{path}{image}', 0)
    converted_img = cv2.cvtColor(img_obj, cv2.COLOR_BGR2RGB)
    extracted = pt.image_to_string(converted_img)
    extracted_text_list += extracted
    extracted_text_list += '\n\n' +  ('*'*100) + '\n\n'
    
print(extracted_text_list)

The program should be easy to understand for even a python beginner, however, I’ll just explain a few stuffs:

So we used a for loop to access all the files in the image folder, then we used opencv to read and convert the image to an opencv image object using cv2.imread() method. The second argument(0) means gray.

Normally opencv will convert the image to BRG by default, so we need to convert it to RGB using the cv2.cvtColor() method so as to get the desired result.

Lastly we added the extracted texts to extracted_text_list variable in a way each extracted text will be separated from each other and easily read when printed out.

BONUS: MAKING OUR CODE REUSABLE.

import pytesseract as pt
import os
import cv2


def extractor():
    #get and store the image path as a variable
    path = input('Enter image below:\n')
    #let's check and confirm the images in our chosen directory/path
    print(f'The image files on the path entered is as follows:\n {os.listdir(path)}')

    #let's create an empty list for the texts we will extract
    extracted_text_list = ''

    #let's write a code to extract and add the texts to our extracted_text_list
    for image in os.listdir(path):
        img_obj = cv2.imread(f'{path}{image}', 0)
        converted_img = cv2.cvtColor(img_obj, cv2.COLOR_BGR2RGB)
        extracted = pt.image_to_string(converted_img)
        extracted_text_list += extracted
        extracted_text_list += '\n\n' +  ('*'*100) + '\n\n'

    print(extracted_text_list)
    
extractor()

So now we can use this same code on any other folder anywhere. We made a function out of our initial program and used the input function to get a path each time we run the function.

I hope you enjoyed this short tutorial on OpenCV in python. Let me know in the comment section if you ran into any bug.

I’ll be making another post on OpenCV in python sometime in the future. Be sure to subscribe to our newsletter to get first-hand notification anytime it’s published and to also get notified for other posts/tutorials.

To super-charge your python coding skills or go from zero to advanced python programmer within a few months with a very flexible, convenient and affordable training click here.

🤞 Don’t miss the tips!

We don’t spam! Read more in our privacy policy

Geoff

Geoff is a python software engineer, web content specialist, tech private trainer and an IT virtual assistant.

This Post Has One Comment

  1. Billy

    Am sure going to explore this opencv module further. Thanks Geoff

Leave a Reply