Extract Text from Image with Python & OpenCV

Python will automatically find and extract text from an image. Yes, Python can do amazing things. Let’s start working on this interesting Python project.

A picture is worth a thousand words

You must have heard the quote many times right! Well, the saying is very true because sometimes the picture says it all.

We have discussed the importance of a single picture so far and now it’s time to learn something more interesting about it. In this python project, we will learn how to extract text content from images using openCV and tesseract. Now let’s see how we can create this.

What is Tesseract?

It is an open-source engine for optical character recognition (OCR). It efficiently reads text from images and is very easy to use. As mentioned earlier it is open source so it is free to use.

Prerequisites

To implement this project you should have basic knowledge of:

  1. Python
  2. Tessaract
  3. OpenCV.
  4. Tkinter

Download Project Code

Before proceeding ahead, please download the source code of Text Extraction Project: Extract Text from Image with Python.

Steps to start text extraction

Let’s start the text detection and extraction project development

Install required libraries

To install the libraries use pip installer from the command prompt / terminal:

Pip install opencv-python
Pip install pytesseract
pip install tkinter

Create main.py

Create main.py file and add the following code

Code:

from tkinter import *
from tkinter import filedialog
from PIL import ImageTk, Image
import cv2
import pytesseract 

pytesseract.pytesseract.tesseract_cmd = 'C:\\Program Files (x86)\\Tesseract-OCR\\tesseract.exe'

root = Tk()
root.title('TechVidvan Text from image project') 

newline= Label(root)
uploaded_img=Label(root)
scrollbar = Scrollbar(root)
scrollbar.pack( side = RIGHT, fill = Y )


def extract(path):
    Actual_image = cv2.imread(path)
    Sample_img = cv2.resize(Actual_image,(400,350))
    Image_ht,Image_wd,Image_thickness = Sample_img.shape
    Sample_img = cv2.cvtColor(Sample_img,cv2.COLOR_BGR2RGB)
    texts = pytesseract.image_to_data(Sample_img) 
    mytext=""
    prevy=0
    for cnt,text in enumerate(texts.splitlines()):
        if cnt==0:
            continue
        text = text.split()
        if len(text)==12:
            x,y,w,h = int(text[6]),int(text[7]),int(text[8]),int(text[9])
            if(len(mytext)==0):
                prey=y
            if(prevy-y>=10 or y-prevy>=10):
                print(mytext)
                Label(root,text=mytext,font=('Times',15,'bold')).pack()
                mytext=""
            mytext = mytext + text[11]+" "
            prevy=y
    Label(root,text=mytext,font=('Times',15,'bold')).pack()

def show_extract_button(path):
    extractBtn= Button(root,text="Extract text",command=lambda: extract(path),bg="#2f2f77",fg="gray",pady=15,padx=15,font=('Times',15,'bold'))
    extractBtn.pack()

def upload():
    try:
        path=filedialog.askopenfilename()
        image=Image.open(path)
        img=ImageTk.PhotoImage(image)
        uploaded_img.configure(image=img)
        uploaded_img.image=img
        show_extract_button(path)
    except:
        pass  

uploadbtn = Button(root,text="Upload an image",command=upload,bg="#2f2f77",fg="gray",height=2,width=20,font=('Times',15,'bold')).pack()
newline.configure(text='\n')
newline.pack()
uploaded_img.pack()

root.mainloop()

Explanation:

  1. Import all the required libraries (opencv, tkinter, tesseract)
  2. Provide the location of the tesseract.exe file.
  3. Tkinter provides GUI functionalities: open an image dialog box so user can upload an image
  4. Let’s jump to the extract function which takes the path of the image as a parameter
    • In this function, we’ll read the image using cv2.imread. We will also resize the image so that we can get well-formatted output for all different sizes of input images.
    • Tesseract works on RGB images and opencv reads an image as BGR image, so we need to convert the image and then call tesseract functions on the image. Here,the conversion is done using cv2.cvtCOLOR().
    • we have stored height, width, and thickness of the input image using img.shape for later use.
    • After the pre-processing, call image_to_data() function of tesseract which returns a string (of extracted text from the image0.
    • Print the whole string for better understanding.
    • The string is a multiline string, where each line contains extracted text but its first line (starting from zero) contains headings that are not useful for us, so we will skip the very first line.
    • Now, split the string to get the extracted text and finally print the extracted text on the screen.

Project Output

text extraction project

Summary

In this article, we have successfully developed a project which automatically detects and extracts text from images very efficiently using inbuilt functions of pytesseract and opencv.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.