Skip to content

How to Fix Python Tesseract OCR Error

DodaTech Updated 2026-06-24 2 min read

In this tutorial, you'll learn about How to Fix Python Tesseract OCR Error. We cover key concepts, practical examples, and best practices.

Pytesseract raises TesseractNotFoundError or produces garbage text when Tesseract is not installed, the language pack is missing, or the image needs preprocessing.

The Wrong Way

import pytesseract
from PIL import Image

text = pytesseract.image_to_string(Image.open("scan.jpg"))
print(text)

If Tesseract is not installed:

pytesseract.pytesseract.TesseractNotFoundError: tesseract is not installed

The Right Way

import pytesseract
from PIL import Image, ImageEnhance, ImageFilter
import re

# Preprocess the image
img = Image.open("scan.jpg")
img = img.convert("L")  # Grayscale
img = ImageEnhance.Contrast(img).enhance(2.0)
img = img.filter(ImageFilter.SHARPEN)

# OCR with configuration
custom_config = r'--oem 3 --psm 6'
text = pytesseract.image_to_string(img, config=custom_config)

# Clean the output
text = re.sub(r'\s+', ' ', text).strip()
print(text)

Clean text is extracted from the image.

Prevention Tips

  • Install Tesseract: sudo apt install tesseract-ocr (Linux) or download from GitHub.
  • Set pytesseract.pytesseract.tesseract_cmd if Tesseract is not in PATH.
  • Install language packs: sudo apt install tesseract-ocr-fra for French.
  • Preprocess images: grayscale, contrast enhancement, threshold.
  • Use --psm (page segmentation mode) appropriate for your image type.

Common Mistakes with tesseract ocr

  1. Mixing let bindings with <- bindings in do notation, producing type errors
  2. Overlapping type class instances that cause GHC to reject the program with ambiguous dispatch errors
  3. Non-exhaustive pattern matches that compile with warnings then crash at runtime

These mistakes appear frequently in real-world PYTHON code. DodaTech's contributors have identified these patterns through analysis of open-source projects and production systems.

Practice Exercise

Write a pure function that safely divides two integers using Maybe, then test it with edge cases like division by zero and negative numbers.

This exercise reinforces the concepts covered in this guide. Try implementing it before checking online solutions.

FAQ

### What is the difference between --psm modes?

PSM (Page Segmentation Mode) tells Tesseract how to analyze the page. --psm 6 assumes a uniform block of text. --psm 3 is fully automatic. --psm 7 treats the image as a single text line.

Why does Tesseract produce wrong characters?

Low-quality images cause OCR errors. Increase DPI to at least 300. Use proper contrast. For specific fonts, train Tesseract with .traineddata files or use --oem 1 (LSTM engine).

How do I extract text from scanned PDFs?

Convert PDF pages to images with pdf2image, then OCR each page. Combine the text. Use --psm 1 for automatic page segmentation with OSD (orientation and script detection).

Built by the developers of Doda Browser, DodaZIP, and Durga Antivirus Pro.

Built by the developers of DodaTech

Doda Browser, DodaZIP & Durga Antivirus Pro