Skip to content
Related Articles
Open in App
Not now

Related Articles

How to Recognize Optical Characters in Images in Python?

Improve Article
Save Article
  • Last Updated : 03 Jan, 2023
Improve Article
Save Article

Prerequisite: Pytesseract, OpenCV

In this article, we are going to recognize the character from the images and get text data out of an image. Let’s take a quick introduction to the required module.

  • OpenCV: It is a Python module in which we can do image processing, video capture, and some analysis tools like face detection or object detection. It has C++, Python, Java, and MATLAB interfaces and supports Windows, Linux, Android, and Mac OS.
  • Pytesseract: Python-tesseract is an optical character recognition (OCR) tool for Python. That is, it will recognize and “read” the text embedded in images. Python-tesseract is a wrapper for Google’s Tesseract-OCR Engine. It is also useful as a stand-alone invocation script to tesseract, as it can read all image types supported by the Pillow and Leptonica imaging libraries, including jpeg, png, gif, BMP, tiff, and others. Additionally, if used as a script, Python-tesseract will print the recognized text instead of writing it to a file.


  • Import required module
  • Setting the path of pytesseract.
  • Read the image with cv2.imread()
  • Convert image in RGB format.
  • Extract text from image using pytesseract.image_to_string(img).

We are going to use this image for a demonstration:

Below is the full implementation: 


# importing the libraries
import cv2
import pytesseract
# setting the path of pytesseract exe
# you have to write the location of
# on which your tesseract was installed
pytesseract.pytesseract.tesseract_cmd = 'C:\\Program Files\\Tesseract-OCR\\tesseract.exe'
# Now we will read the image in our program
# you have to put your image path in place of photo.jpg
img = cv2.imread('photo.jpg')
# Our image will read as BGR format,
# So we will convert in RGB format because 
# tesseract can only read in RGB format
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
# For getting the text and number from image
# For displaying the original image
cv2.imshow("result", img)


My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!