tesserocr

2.10.0 · active · verified Thu Apr 16

tesserocr is a simple, Pillow-friendly Python wrapper around the Tesseract-OCR API, built using Cython. It is currently at version 2.10.0 and maintains an active release schedule with several minor and patch updates throughout the year, primarily focusing on Tesseract/Leptonica version upgrades and Python compatibility.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates basic text recognition from an image using `tesserocr.PyTessBaseAPI` for more control and `tesserocr.image_to_text` for simplicity. It includes a base64-encoded image to make the example self-contained and runnable.

import tesserocr
from PIL import Image
from io import BytesIO
import base64

# A simple base64 encoded image containing "Hello World" for a runnable example
# In a real scenario, you'd load an image from file: Image.open("path/to/image.png")
img_data = "iVBORw0KGgoAAAANSUhEUgAAAKMAAABRCAYAAADtL/VCAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAAEnQAABJ0AdFcPaeAAAAABTBMVEX///8AAAD///+xO/FBAAACHklEQVR4Xu2ZzytEQRjHn+3+oBv9B5gC927O4QeYM3Y3B38B3L2ZzR+gE2fPnsR/mD0Y+G9zZmbO+J0P921m3zMzt9O9eA+i+3zP/M78e3/qXWwP+qL1l1gYGBgYGBgYGBgYGBgYGBgYGBgYGBgYGBgYGBgYGBgYGBgYGBgYGBgYGBgYGBgYGBgYGBgYGBgYGBgYGBgYGBgYGBgYGBgYGBgYGBgYGBgYGBgYGBgYGBgYGBgYGBgYGBgYGBgYGBgYGBgYGBgYGBgYGBgYGBgYGBgYGBgYGBgYGBgYGBgYGBgYGBgYGBgYGBgYGBgYGBgYGBgYGBgYGBgYGBgYGBgYGBgYGBgYGBgYGBgYGBgYGBgYGBgYGBgYGBgYGBgYGBgYGBgYGBgYGBgYGBgYGBgYGBgYGBgYGBgYGBgYGBgYGBgYGBgYGBgYGBgYGBgYGBgYGBgYGBgYGBgYGBgYGBgYGBgYGBgYGBgYGBgYGBgYGBgYGBgYGBgYGBgYGBgYGBgYGBgYGBgYGBgYGBgYGBgYGBgYGBgYGBgYGBgYGBgYGBgYGBgYGBgYGBgYGBgYGBgYGBgYGBgYGBgYGBgYGBgYGBgYGBgYGBgYGBgYGBgYGBgYGBgYGBgYGBgYGBgYGBgYGBgYGBgYGBgYGBgYGBgYGBgYGBgYGBgYGBgYGBgYGBgYGBgYGv80v+gN3m82rF44BAAAAAElFTkSuQmCC"
image_bytes = base64.b64decode(img_data)
image = Image.open(BytesIO(image_bytes))

# Initialize Tesseract API
# You might need to specify lang='eng' or tessdata_dir='path/to/tessdata'
# if Tesseract is not configured globally or language files are not found.
api = tesserocr.PyTessBaseAPI(lang='eng')
api.SetImage(image)
text = api.GetUTF8Text()
confidence = api.MeanTextConf()
api.End()

print(f"Detected Text: {text.strip()}")
print(f"Confidence: {confidence}")

# Shorter convenience function for quick OCR
text_short = tesserocr.image_to_text(image, lang='eng')
print(f"Detected Text (short function): {text_short.strip()}")

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

view raw JSON →