Using Tesseract OCR from Python

pytesseract is the usual Python API for Tesseract: install the Tesseract program, run pip install pytesseract pillow, then call pytesseract.image_to_string(Image.open("scan.png")). Use image_to_data for word boxes and confidence, lang="deu" (or another code) for other languages, and --psm to tell it how the page is laid out. If you'd rather install nothing, or your images are phone photos, an OCR API is the alternative; ours costs $0.03 per image.

Try it free Image to Text API (OCR) — read text from photos and scans · $0.03 / call

Install

# macOS
brew install tesseract
# Ubuntu / Debian, with German as an extra language
sudo apt install tesseract-ocr tesseract-ocr-deu
# Windows: install a Tesseract build, then tell pytesseract where it is:
#   pytesseract.pytesseract.tesseract_cmd = r"C:\Program Files\Tesseract-OCR\tesseract.exe"

pip install pytesseract pillow

The calls you'll actually use

import pytesseract
from PIL import Image

img = Image.open("scan.png")

# plain text, English plus German, one uniform block of text
text = pytesseract.image_to_string(img, lang="eng+deu", config="--psm 6")

# every word with its box and confidence
data = pytesseract.image_to_data(img, output_type=pytesseract.Output.DICT)
sure = [w for w, c in zip(data["text"], data["conf"]) if w.strip() and float(c) > 60]

# which language packs are installed
print(pytesseract.get_languages())

# a searchable PDF of the image
open("scan.pdf", "wb").write(pytesseract.image_to_pdf_or_hocr(img, extension="pdf"))

Page segmentation modes that matter

--psmUse it for
3 (default)A full page; Tesseract works out the layout
4One column of text in different sizes, like a receipt
6One even block of text, like a paragraph crop
7A single line, such as a number plate or a label
11Scattered text with no order, like a poster

Clean the image first

Tesseract likes black text on a white background at a decent size. A few lines of Pillow often help more than any setting:

img = Image.open("photo.jpg").convert("L")                 # greyscale
if img.width < 1500:
    img = img.resize((img.width * 2, img.height * 2))      # small text: upscale
img = img.point(lambda p: 255 if p > 160 else 0)          # black and white
print(pytesseract.image_to_string(img))

When to stop tuning

In those cases, test an OCR API on the same images. Ours takes a link or base64 and returns the text in reading order, with tables as Markdown.

The product

Questions

What does TesseractNotFoundError mean?

pytesseract can't find the tesseract program. Install it, or set pytesseract.pytesseract.tesseract_cmd to its full path.

pytesseract or tesserocr?

pytesseract calls the tesseract command for each image: easy to install, a little slower. tesserocr binds to the Tesseract library directly: faster for large batches, harder to install.

Can Tesseract read a PDF?

Not directly. Turn the pages into images first (for example with pdf2image or pdftoppm), then OCR each image.

Related

Updated 2026-12-01