Sub topic 6
Choosing a Page Segmentation Mode
This chapter assumes basic Python, pytesseract, and familiarity with images as pixel arrays. A page segmentation mode, or PSM, tells Tesseract how text is arranged in an image. Choosing the wrong mode can make a clear image produce missing, scrambled, or incorrectly ordered text.
Why layout matters
Tesseract does more than recognize individual characters. It first analyzes the page layout and groups pixels into blocks, lines, words, and characters. The PSM controls how much structure Tesseract expects to find.
For example, a scanned report may contain one large text column. A street photograph may contain a few unrelated words at different positions. A cropped license plate may contain only one line. These images need different assumptions.
The default mode is PSM 3, which performs fully automatic page segmentation without orientation and script detection. It is a reasonable starting point for a normal page, but it is not the best choice for every crop.
Common PSM choices
| PSM | Layout assumption | Useful example |
|---|---|---|
| 3 | Fully automatic page segmentation | A normal scanned page |
| 4 | One column of variable-size text | A single newspaper-like column |
| 6 | One uniform block of text | A cropped receipt section |
| 7 | One text line | A serial number or form field |
| 8 | One word | A cropped label |
| 10 | One character | A single large character |
| 11 | Sparse text in different locations | Text on a sign or photograph |
| 12 | Sparse text with orientation detection | Rotated or scattered scene text |
| 13 | Raw single line | A carefully cropped line where layout analysis hurts |
PSM 6 is often a good choice for a tightly cropped block. PSM 7 is better when the crop contains exactly one line. PSM 11 can find separate words without assuming that they form a regular paragraph.
The modes are assumptions, not quality settings. A higher mode number does not mean higher accuracy.
Setting the mode in Python
Pass the mode through Tesseract's configuration string. The following script runs several modes on the same image so you can compare their output.
from pathlib import Path
import pytesseract
from PIL import Image
image_path = Path("crop.png")
image = Image.open(image_path)
for psm in (3, 6, 7, 11, 13):
config = f"--psm {psm}"
text = pytesseract.image_to_string(image, config=config)
print(f"\n--- PSM {psm} ---")
print(text.strip())Use an image crop that matches the assumption you are testing. If crop.png contains a whole page, PSM 7 will force the page into one line and usually fail. If it contains one line, PSM 3 may spend effort searching for a page structure that does not exist.
A practical selection process
Start with the simplest description of the image:
- Use PSM 3 for a normal page with an unknown layout.
- Use PSM 4 when the image contains one column.
- Use PSM 6 for one rectangular block with several lines.
- Use PSM 7 or 13 for one line.
- Use PSM 8 for one word and PSM 10 for one character.
- Use PSM 11 or 12 when words are scattered across the image.
Then inspect the result rather than trusting one run. Compare line order, missing words, and unwanted text. For a production system, test several candidate modes on representative images and select the mode that gives the best measured accuracy.
Orientation and language considerations
Some modes include orientation and script detection, while others assume that the text is already upright. If the image may be rotated, test PSM 12 or use a separate rotation step. Orientation detection can also add work and may be unreliable on very small text.
PSM does not choose the recognition language. Set languages separately with lang, such as lang="eng" for English. A correct PSM with the wrong language data can still produce poor results.
Exercise
Which PSM should you try first for an image cropped tightly around one horizontal line of text, and why?
Show answer
Try PSM 7 because it tells Tesseract to treat the image as one text line. Compare it with PSM 13 if normal line-layout analysis produces unwanted spacing or missing characters.