OCR With Python
Advanced pytesseract configurations for different document types.
8 min•By Priygop Team•Updated 2026
Tesseract Configuration Options
Tesseract Configuration Options
import pytesseract
import cv2
image = cv2.imread("document.jpg")
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
# PSM (Page Segmentation Mode) options:
# --psm 3 Fully automatic page segmentation (default)
# --psm 6 Assume a single uniform block of text
# --psm 7 Treat the image as a single text line
# --psm 8 Treat the image as a single word
# --psm 10 Treat the image as a single character
# --psm 11 Sparse text — find as much text as possible
# OEM (OCR Engine Mode):
# --oem 0 Legacy Tesseract engine
# --oem 1 Neural nets LSTM engine (recommended, more accurate)
# --oem 3 Default, based on what is available
# Single line (e.g., a label or caption)
single_line = pytesseract.image_to_string(gray, config="--psm 7 --oem 1")
print("Single line:", single_line.strip())
# Document block (e.g., a paragraph)
block = pytesseract.image_to_string(gray, config="--psm 6 --oem 1")
print("Block text:", block[:100])
# Extract only digits (e.g., from a number or price)
digits_only = pytesseract.image_to_string(
gray,
config="--psm 8 --oem 1 -c tessedit_char_whitelist=0123456789"
)
print("Digits:", digits_only.strip())Diagram
Loading diagram…
Machine Learning follows a structured pipeline from data to deployment