Pytesseract

0.3.13 · active · verified Sun Apr 05

Pytesseract is a Python wrapper for Google's Tesseract-OCR Engine, providing an optical character recognition (OCR) tool for Python. It enables users to recognize and extract text embedded in images, supporting various image types through the Pillow library. The project is actively maintained with regular updates to support newer Python versions and improve functionality. Its current version is 0.3.13.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to use `pytesseract` to extract text from an image. It first creates a simple in-memory image with text using Pillow, then uses `pytesseract.image_to_string()` to perform OCR. It also shows how to check the installed Tesseract version. Remember that the Tesseract-OCR engine itself must be installed on your system for `pytesseract` to function.

from PIL import Image, ImageDraw, ImageFont
import pytesseract
import os

# NOTE: Ensure Tesseract-OCR is installed on your system and its executable is in your PATH.
# If not, you might need to specify the path to tesseract.exe:
# pytesseract.pytesseract.tesseract_cmd = r'C:\Program Files\Tesseract-OCR\tesseract.exe'

# Create a dummy image for demonstration
img_width, img_height = 400, 100
img = Image.new('RGB', (img_width, img_height), color = 'white')
draw = ImageDraw.Draw(img)

try:
    # Try to use a common system font
    font = ImageFont.truetype("arial.ttf", 24)
except IOError:
    # Fallback if Arial is not found (e.g., on some Linux systems without it)
    font = ImageFont.load_default()

text = "Hello, Pytesseract OCR!"
draw.text((50, 30), text, fill='black', font=font)

# Perform OCR on the image
extracted_text = pytesseract.image_to_string(img)

print(f"Extracted Text: {extracted_text.strip()}")

# Example of getting Tesseract version
tesseract_version = pytesseract.get_tesseract_version()
print(f"Tesseract Version: {tesseract_version}")

view raw JSON →