Reading Text from the Image using Tesseract

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

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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)

chevron_right


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 Grey Scale 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 Grey Scale Image Displayed. (p.png)

input image

p

Output:

reading text from image

Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.