Open In App

Reading Text from the Image using Tesseract

Improve
Improve
Like Article
Like
Save
Share
Report

Pytesseract or Python-tesseract is an Optical Character Recognition (OCR) tool for python. It will read and recognize the text in images, license plates, etc. Here, we will use the tesseract package to read the text from the given image. 
 

Mainly, 3 simple steps are involved here as shown below:-  

  • Loading an Image saved from the computer or download it using a browser and then loading the same. (Any Image with Text).
  • Binarizing the Image (Converting Image to Binary).
  • We will then Pass the Image through the OCR system.

Implementation:

The following python code represents the Localizing of the Text and correctly guessing the text written in the image. 
 

Python3




# We import the necessary packages
#import the needed packages
import cv2
import os,argparse
import pytesseract
from PIL import Image
 
#We then Construct an Argument Parser
ap=argparse.ArgumentParser()
ap.add_argument("-i","--image",
                required=True,
                help="Path to the image folder")
ap.add_argument("-p","--pre_processor",
                default="thresh",
                help="the preprocessor usage")
args=vars(ap.parse_args())
 
#We then read the image with text
images=cv2.imread(args["image"])
 
#convert to grayscale image
gray=cv2.cvtColor(images, cv2.COLOR_BGR2GRAY)
 
#checking whether thresh or blur
if args["pre_processor"]=="thresh":
    cv2.threshold(gray, 0,255,cv2.THRESH_BINARY| cv2.THRESH_OTSU)[1]
if args["pre_processor"]=="blur":
    cv2.medianBlur(gray, 3)
     
#memory usage with image i.e. adding image to memory
filename = "{}.jpg".format(os.getpid())
cv2.imwrite(filename, gray)
text = pytesseract.image_to_string(Image.open(filename))
os.remove(filename)
print(text)
 
# show the output images
cv2.imshow("Image Input", images)
cv2.imshow("Output In Grayscale", gray)
cv2.waitKey(0)


Now, follow the below steps to successfully Read Text from an image: 

  • Save the code and the image from which you want to read the text in the same file.
  • Open Command Prompt.Go to the location where the code file and image is saved.
  • Execute the command below to view the Output.

Example 1:

Execute the command below to view the Output.

python  tesseract.py --image Images/title.png

We have The Original Image displayed.

input image

title

We have the GrayScale Image Displayed. (p.png)

input image

p

Output:

reading text from image

Example 2:

Execute the command below to view the Output.

python  tesseract.py --image Images/OCR.png

We have The Original Image displayed.

input image

OCR

We have the GrayScale Image Displayed. (p.png)

input image

p

Output:

reading text from image



Last Updated : 01 Dec, 2022
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads