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 packagesimport cv2import os,argparseimport pytesseractfrom PIL import Image #We then Construct an Argument Parserap=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 textimages=cv2.imread(args["image"]) #convert to grayscale imagegray=cv2.cvtColor(images, cv2.COLOR_BGR2GRAY) #checking whether thresh or blurif 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 memoryfilename = "{}.jpg".format(os.getpid())cv2.imwrite(filename, gray)text = pytesseract.image_to_string(Image.open(filename))os.remove(filename)print(text) # show the output imagescv2.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.

title
We have the Grey Scale Image Displayed. (p.png)

p
Output:

Example 2:
Execute the command below to view the Output.
python tesseract.py --image Images/OCR.png
We have The Original Image displayed.
OCR
We have the Grey Scale Image Displayed. (p.png)

p
Output:

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.



