# Tesseract OCR in Python: Setup, Examples, and Common Errors

> Set up Tesseract OCR in Python with pytesseract, run a tested image_to_string example, and fix missing binaries, language data, and empty results.

Canonical: https://reducto.ai/guides/tesseract-ocr-python

## The short answer

To use Tesseract OCR in Python, install two things: the Tesseract engine (a native program from your OS package manager or installer) and the [pytesseract](https://github.com/madmaze/pytesseract) wrapper from pip. Then load an image with Pillow and call `pytesseract.image_to_string()`. Installing only the pip package is the most common reason a first script fails.

The example below was tested on Ubuntu 24.04 with Tesseract 5.3.4, pytesseract 0.3.13, Pillow 12.1.0, and Python 3.12. Tesseract is a dependable CPU baseline for clean printed text, as our [comparison of OCR models](https://reducto.ai/guides/best-ocr-models-accuracy-speed-cost) notes. If you need table structure, reading order, or a managed API, see the managed parsing section at the end of this guide.

## Tesseract engine versus the pytesseract wrapper

Tesseract is an open-source OCR engine written in C++. It runs as a command-line program and reads image files using its own trained language data files (`.traineddata`).

pytesseract does not perform OCR itself. It writes your image to a temporary file, runs the `tesseract` executable, and returns the output as a Python string, dictionary, or DataFrame. That design explains most setup problems: if Python cannot find the executable, or the executable cannot find its language data, pytesseract raises an error even though `pip install` succeeded.

## Install Tesseract and pytesseract

### Step 1: install the Tesseract engine

The [official installation guide](https://tesseract-ocr.github.io/tessdoc/Installation.html) lists packages for each platform. Package versions differ: Ubuntu 22.04's default repository ships Tesseract 4.1.1, while Ubuntu 24.04 ships 5.3.4.

<Table html={"<table>\n  <thead>\n    <tr>\n      <th>Platform</th>\n      <th>Install the engine</th>\n      <th>Add a language (German example)</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <td>Ubuntu or Debian</td>\n      <td><code>sudo apt install tesseract-ocr</code></td>\n      <td><code>sudo apt install tesseract-ocr-deu</code></td>\n    </tr>\n    <tr>\n      <td>macOS (Homebrew)</td>\n      <td><code>brew install tesseract</code></td>\n      <td><code>brew install tesseract-lang</code> (all languages)</td>\n    </tr>\n    <tr>\n      <td>macOS (MacPorts)</td>\n      <td><code>sudo port install tesseract</code></td>\n      <td><code>sudo port install tesseract-deu</code></td>\n    </tr>\n    <tr>\n      <td>Windows</td>\n      <td>Run the <a href=\"https://github.com/UB-Mannheim/tesseract/wiki\">UB Mannheim installer</a></td>\n      <td>Select extra languages during setup</td>\n    </tr>\n  </tbody>\n</table>"} />

Confirm the engine works before touching Python:

```bash
tesseract --version
tesseract --list-langs
```

### Step 2: install the Python wrapper

Use a virtual environment and pin versions so your results are reproducible:

```bash
python3 -m venv .venv
source .venv/bin/activate
pip install pytesseract==0.3.13 pillow==12.1.0
```

### Step 3: configure the path if needed

On Linux and macOS, package managers usually put `tesseract` on your `PATH`, so no configuration is needed. On Windows, the installer does not always add it to `PATH`. Point pytesseract at the executable before any OCR call:

```python
import pytesseract

# Use the folder you chose in the installer; this is a common default
pytesseract.pytesseract.tesseract_cmd = r"C:\Program Files\Tesseract-OCR\tesseract.exe"
```

On macOS, run `which tesseract` to get the exact path, which differs between Intel and Apple Silicon Homebrew installs.

## Run a minimal image_to_string example

We generated a synthetic invoice image with Pillow (no real customer data) and ran this script:

```python
import pytesseract
from PIL import Image

print(pytesseract.get_tesseract_version())
text = pytesseract.image_to_string(Image.open("invoice.png"))
print(text)
```

Real output, trimmed of trailing blank lines:

```text
5.3.4
ACME Supply Co.

Invoice INV-20481

Date: 2026-11-02

WidgetA 2x14.50 29.00
Total due: 49.00 USD
```

Most of the text is right, but the line item lost its spacing: `Widget A   2 x 14.50` became `WidgetA 2x14.50`. Plain text output has no columns, so any table structure has to be rebuilt by your own code.

`image_to_string()` also accepts a file path or a NumPy array, and a `lang` argument such as `lang="eng+deu"` for multiple languages. For word-level boxes and confidence values, use `image_to_data()`.

## Fix common Tesseract errors in Python

We reproduced each error below in a clean Ubuntu 24.04 container with the versions listed above.

### TesseractNotFoundError: tesseract is not installed or it's not in your PATH

This is the classic "tesseract not found" error. We triggered it by installing pytesseract before the engine:

```text
pytesseract.pytesseract.TesseractNotFoundError: tesseract is not installed or it's not in your PATH. See README file for more information.
```

**Fix:** install the engine (Step 1), then open a new terminal and run `tesseract --version`. If that works in a terminal but Python still fails, which is common on Windows and inside IDEs, set `pytesseract.pytesseract.tesseract_cmd` to the full path of the executable. In Docker images, add the engine package to the image, not just `requirements.txt`.

### Failed loading language

Passing a language whose data file is not installed fails with a `TesseractError`:

```text
pytesseract.pytesseract.TesseractError: (1, 'Error opening data file /usr/share/tesseract-ocr/5/tessdata/deu.traineddata Please make sure the TESSDATA_PREFIX environment variable is set to your "tessdata" directory. Failed loading language \'deu\' Tesseract couldn\'t load any languages! Could not initialize tesseract.')
```

**Fix:** install the language package (`sudo apt install tesseract-ocr-deu` on Ubuntu), then confirm it appears in `tesseract --list-langs`. After that, our German test image returned `Größe: Übergröße für Straßen` exactly. If you download `.traineddata` files manually from the [official data files](https://tesseract-ocr.github.io/tessdoc/Data-Files.html), put them in the `tessdata` directory shown by `--list-langs`, or point `TESSDATA_PREFIX` at the folder that contains them.

### image_to_string returns an empty string

Empty output usually means Tesseract's page segmentation found no text block, not that the call failed. On a tightly cropped image of `Total 49.00` with no margin, the default settings returned `''`. Adding a 20-pixel white border fixed it:

```python
from PIL import ImageOps

padded = ImageOps.expand(image, border=20, fill=255)
text = pytesseract.image_to_string(padded)  # 'Total 49.00\n'
```

Other causes listed in Tesseract's [image quality guide](https://tesseract-ocr.github.io/tessdoc/ImproveQuality.html) include very small text (rescale the image), light text on a dark background (invert it), noise, and skew. Choosing a page segmentation mode that matches the image also helps.

## Select a page segmentation mode

The page segmentation mode (PSM) tells Tesseract what layout to expect. Pass it through `config`:

```python
text = pytesseract.image_to_string(image, config="--psm 7")
```

On a downscaled 75 x 15 pixel label from our tests, the default returned `''` while `--psm 7` returned `INV-20481`. Run `tesseract --help-psm` for the full list. These modes cover most cases:

<Table html={"<table>\n  <thead>\n    <tr>\n      <th>PSM</th>\n      <th>Tesseract description</th>\n      <th>Use it for</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <td>3</td>\n      <td>Fully automatic page segmentation (default)</td>\n      <td>Full pages of ordinary text</td>\n    </tr>\n    <tr>\n      <td>4</td>\n      <td>A single column of text of variable sizes</td>\n      <td>Receipts and simple single-column lists</td>\n    </tr>\n    <tr>\n      <td>6</td>\n      <td>A single uniform block of text</td>\n      <td>A cropped paragraph or text region</td>\n    </tr>\n    <tr>\n      <td>7</td>\n      <td>A single text line</td>\n      <td>Labels, form fields, and ID numbers</td>\n    </tr>\n    <tr>\n      <td>8</td>\n      <td>A single word</td>\n      <td>Isolated words or codes</td>\n    </tr>\n    <tr>\n      <td>11</td>\n      <td>Sparse text, in no particular order</td>\n      <td>Scattered text such as diagrams and signs</td>\n    </tr>\n  </tbody>\n</table>"} />

PSM changes how Tesseract finds text, not how it orders or structures it. Mode 11 finds scattered words but does not return them in reading order, and no mode returns table cells.

## When to use a managed parsing API instead

Tesseract is a good choice for clean, printed, single-column images that you can run locally at no license cost. Its limits show up on real business documents: multi-column layouts, tables, forms, poor scans, and PDFs, which you must render to images first. You end up building preprocessing, layout detection, table reconstruction, and confidence handling around the engine.

[Reducto Parse](https://reducto.ai/parse) is a managed API for that layer. According to the [Parse documentation](https://docs.reducto.ai/parse/overview), it returns text, tables, figures, and layout with bounding boxes, organized into chunks for LLM and RAG pipelines. Tables come back as HTML or Markdown blocks, and each block carries a confidence label, as described in the [response format](https://docs.reducto.ai/parse/response-format). It accepts PDFs, images, spreadsheets, and office files, so you do not need a separate PDF-to-image step.

Before switching, compare both tools on your own documents using the metrics in our guide to [OCR accuracy](https://reducto.ai/guides/most-accurate-ocr-software). To see how your files parse, upload a sample in [Reducto Studio](https://studio.reducto.ai/).

## Frequently asked questions

### What is the difference between Tesseract and pytesseract?

Tesseract is the OCR engine that recognizes text. pytesseract is a Python wrapper that calls the installed `tesseract` executable and returns its output. You need both.

### Does pip install pytesseract install Tesseract?

No. pip installs only the wrapper. Install the engine with your OS package manager or the Windows installer, then check it with `tesseract --version`.

### Can pytesseract read PDFs?

Not directly. Render each PDF page to an image first, then pass the images to pytesseract. pytesseract can also create a searchable PDF from an image with `image_to_pdf_or_hocr()`.

### How do I get confidence scores from Tesseract in Python?

Call `pytesseract.image_to_data()`. It returns each recognized word with its bounding box and a confidence value, which you can use to flag low-confidence words for review.

### Can Tesseract extract tables?

Tesseract returns text and word positions, not table structure. You can rebuild simple grids from word coordinates. For complex or multi-page tables, a layout-aware parser such as [Reducto](https://reducto.ai/parse) returns table structure directly.
