Last Updated : 23 Jul, 2025
In the age of digital transformation, extracting information from images and scanned documents has become a crucial task.
Optical Character Recognition (OCR) is a technology that enables this by converting different types of documents, such as scanned paper documents, PDF files, or images taken by a digital camera, into editable and searchable data. The Pytesseract module, a Python wrapper for Google's Tesseract-OCR Engine, is one of the most popular tools for this purpose.
What is Pytesseract?Pytesseract is an OCR tool for Python, which enables developers to convert images containing text into string formats that can be processed further. It is essentially a Python binding for Tesseract, which is one of the most accurate open-source OCR engines available today.
Steps to Download and Configure Tesseract-OCR 1. Download and Install Tesseract-OCRTesseract is a free and open-source OCR (Optical Character Recognition) engine.
For Windows:tesseract-ocr-setup.exe
) from the releases section.brew install tesseractFor Linux (Ubuntu/Debian):
sudo apt update2. Add Tesseract to the Environment Variables For Windows:
sudo apt install tesseract-ocr
Path
variable, then click Edit.tesseract.exe
file (usually located in C:\Program Files\Tesseract-OCR\
).Open a terminal and add the Tesseract path to the shell configuration file (.bashrc
, .zshrc
, or .bash_profile
):
export PATH="/usr/local/bin/tesseract:$PATH"
Save the file and run the following command to apply changes:
source ~/.bashrc # or source ~/.zshrc3. Verify Tesseract Installation
After adding Tesseract to our environment variables, open a terminal (or Command Prompt on Windows) and type:
tesseract --versioncheck tesseract version 4. Install Pytesseract:
To use Tesseract with Python, we also need to install the pytesseract
package, which acts as a Python wrapper for Tesseract.
Let's install pytesseract using pip:
pip install pytesseract
Verify the installation:
python -c "import pytesseract; print(pytesseract.get_tesseract_version())"Check pytesseract version Usage of Pytesseract With Practical Examples
In the first and second example, we have used the first image and for the third example we have used the second image.
Example 1: Basic Text Extraction from an ImageExplanation : This simple example shows how to open an image and use Pytesseract to extract the text. The image_to_string method converts the image into text.
Python
from PIL import Image
import pytesseract
# Load an image
img = Image.open('image.png')
# Extract text from image
text = pytesseract.image_to_string(img)
print(text)
Output
Reading text from image using pytessact Example 2 : Extracting Text from a Specific Area in an ImageExplanation: Sometimes, we may need to extract text from a specific area of the image. This example shows how to crop a region of interest (ROI) from the image and extract text from that region.
Python
from PIL import Image
import pytesseract
# Load an image
img = Image.open('image.png')
# Define the region of interest (left, upper, right, lower)
roi = img.crop((50, 50, 300, 300))
# Extract text from the cropped region
text = pytesseract.image_to_string(roi)
print(text)
Output:
Extracting Text from a Specific part of the image Example 3: Text Extraction from a Noisy Image with Pre-processingExplanation : In this example, the image is first converted to grayscale to simplify the data. A median filter is then applied to reduce noise, which is especially helpful for images with a lot of background noise. After that, the contrast of the image is enhanced to make the text more distinguishable from the background. Finally, the pre-processed image is passed to Pytesseract for text extraction.
Python
from PIL import Image, ImageFilter, ImageEnhance
import pytesseract
# Load an image with noise or low contrast
img = Image.open('noisy_image.jpg')
# Convert the image to grayscale
img = img.convert('L')
# Apply a median filter to reduce noise
img = img.filter(ImageFilter.MedianFilter())
# Enhance the image contrast
enhancer = ImageEnhance.Contrast(img)
img = enhancer.enhance(2)
# Extract text from the pre-processed image
text = pytesseract.image_to_string(img)
print(text)
Output
Reading Text from a noisy image using pytesseract Advantages of Pytesseract ModulePytesseract is a powerful and accessible tool for anyone looking to incorporate OCR functionality into their Python projects. While it has its limitations, particularly with handwritten text and complex layouts, it excels in extracting text from images and printed documents with high accuracy. Whether we're working on digitizing documents or extracting text from images in multiple languages, Pytesseract provides a simple and effective solution.
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4