Hassan Agmir Hassan Agmir

How to Extract Text from Images with Python

Hassan Agmir
How to Extract Text from Images with Python

In today's world, images contain a vast amount of textual information. Whether you're scanning documents, processing receipts, or extracting data from forms, the ability to extract text from images using Python has become a valuable tool. Python provides various libraries and methods to achieve Optical Character Recognition (OCR), enabling developers to automate the process of extracting text from images.

In this article, we'll explore how to extract text from images using Python, primarily leveraging the pytesseract library, which is an open-source Python wrapper for Google's Tesseract-OCR engine. We will walk you through setting up your environment, processing images, and handling various use cases.

Table of Contents

  1. Introduction to OCR
  2. Setting Up Your Python Environment
  3. Installing Tesseract and pytesseract
  4. Extracting Text from Images
  5. Preprocessing Images for Better Accuracy
  6. Handling Multilingual Text
  7. Using Custom OCR Configurations
  8. Advanced Use Cases: PDF and Handwritten Text
  9. Conclusion

1. Introduction to OCR

Optical Character Recognition (OCR) is the process of converting images of typed, handwritten, or printed text into machine-readable text. OCR is widely used in applications like automated data entry, digitizing documents, license plate recognition, and more.

The Python ecosystem has a few well-known libraries that simplify the OCR process. The most popular is pytesseract, which is built on Google's open-source Tesseract engine. Tesseract is highly effective in recognizing characters from various languages and is capable of handling complex text extraction tasks.

2. Setting Up Your Python Environment

Before diving into the actual code, ensure your Python environment is set up properly. Here’s a checklist of the tools and libraries you'll need:

  • Python 3.x
  • pytesseract library (Python wrapper for Tesseract OCR)
  • PIL (Python Imaging Library) or OpenCV for image processing
  • Tesseract-OCR engine (needs to be installed separately)

3. Installing Tesseract and pytesseract

Step 1: Install Tesseract-OCR

Since pytesseract is a wrapper for Tesseract, you’ll need to install the Tesseract-OCR engine separately.

  • On Windows: Download the Tesseract executable from GitHub. After downloading, make sure to add Tesseract to your system’s PATH.
  • On macOS: You can install Tesseract via Homebrew by running:
  • brew install tesseract
  • On Linux: You can install Tesseract using apt:
  • sudo apt-get install tesseract-ocr

Step 2: Install pytesseract

Now, you can install pytesseract and Pillow (for image processing) using pip:

pip install pytesseract pillow

Optionally, you can also use OpenCV for advanced image manipulation:

pip install opencv-python

4. Extracting Text from Images

Once the installation is complete, you can start writing Python code to extract text from images.

Here’s a simple example:

import pytesseract
from PIL import Image

pytesseract.pytesseract.tesseract_cmd = r'C:\Program Files\Tesseract-OCR\tesseract.exe'

image = Image.open('sample_image.png')

text = pytesseract.image_to_string(image)

print(text)

This script will extract and print the text from sample_image.png. You can replace the image path with any image containing text.

5. Preprocessing Images for Better Accuracy

Sometimes, images may contain noise, distortion, or unwanted elements that reduce OCR accuracy. To improve results, you can preprocess the image using techniques like resizing, grayscale conversion, or thresholding.

Here’s how you can preprocess an image with OpenCV:

import cv2
import pytesseract

image = cv2.imread('sample_image.png')

gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

_, thresh_image = cv2.threshold(gray_image, 150, 255, cv2.THRESH_BINARY)

cv2.imwrite('preprocessed_image.png', thresh_image)

text = pytesseract.image_to_string(thresh_image)

print(text)

6. Handling Multilingual Text

Tesseract supports multiple languages, but you need to specify the correct language while performing OCR. For instance, if you want to extract text in Spanish or Arabic, you’ll need to download the language data files.

Example of specifying a language (Spanish):

text = pytesseract.image_to_string(image, lang='spa')
print(text)

To download and install additional language packs, follow the instructions on the Tesseract GitHub page.

7. Using Custom OCR Configurations

Tesseract allows you to tweak OCR performance by passing custom configuration options. For example, you can specify character white lists, DPI settings, or use Page Segmentation Modes (PSM).

Example of using a custom configuration:

config = '--oem 3 --psm 6 outputbase digits'
text = pytesseract.image_to_string(image, config=config)

print(text)

8. Advanced Use Cases: PDF and Handwritten Text

Extracting Text from PDFs

For PDF files, you can use pdf2image to convert each page into an image and then apply OCR.

pip install pdf2image

Here’s a quick example:

from pdf2image import convert_from_path
import pytesseract

# Convert PDF to images
pages = convert_from_path('document.pdf', 300)

# Extract text from each page
for page in pages:
    text = pytesseract.image_to_string(page)
    print(text)

Handwritten Text

Tesseract struggles with handwritten text, but you can try improving results using specific training models for handwriting. There are also alternative models like keras-ocr or Google Cloud Vision API for more accurate results with handwritten documents.

9. Conclusion

In this article, we've covered how to extract text from images using Python, with a focus on pytesseract and its capabilities. We discussed the installation process, essential code snippets, preprocessing techniques to improve OCR accuracy, and handling advanced use cases like PDFs and multilingual text.


Subscribe to my Newsletters

Stay updated with the latest programming tips, tricks, and IT insights! Join my community to receive exclusive content on coding best practices.

© Copyright 2025 by Hassan Agmir . Built with ❤ by Me