New Tool Radar

OCR & Text Layers

How to OCR a Single Line of Text with Tesseract Page Segmentation Mode 7

Use Tesseract's single-line page segmentation mode on a one-line scan, such as a caption or a label, and check the line comes out whole.

Schematic illustration of Tesseract page segmentation mode 7: a caption line reads exactly as printed and the label keeps its number.

Tesseract’s –psm option tells the engine what layout to assume. The manual lists mode 7 as treating the image as a single text line, which fits a caption, a label or a form field — images where the default mode may look for paragraphs that are not there.

Use the practice file

A practice document, practice-single-line.pdf. The file is fictional.

Page Content
1 Caption line: “Harvest festival opens on Saturday”
2 Label line: “Batch 42 — spring harvest”
3 Blank page

Page 1 is the check page: the caption reads “Harvest festival opens on Saturday”.

Check mode 7 on a single line

Expected result. We checked the OCRmyPDF manual and the Tesseract manual. We did not run any tool. The checks below are what to expect if the work is done correctly; run them yourself on the practice file.

1. Render the caption

Render page 1 to an image at 300 dpi.

Expected result: The image holds one line of text.

2. Run Tesseract with –psm 7

Run tesseract caption.png out –psm 7.

Expected result: Tesseract produces a text file with one line.

3. Compare the line

Read the output against the caption.

Expected result: The output reads exactly “Harvest festival opens on Saturday”.

4. Check the label too

Repeat with –psm 7 on page 2.

Expected result: The output reads “Batch 42 — spring harvest”, with the number intact.

Verdict table

Check Expected result Pass?
Render the caption The image holds one line of text. —
Run Tesseract with –psm 7 Tesseract produces a text file with one line. —
Compare the line The output reads exactly “Harvest festival opens on Saturday”. —
Check the label too The output reads “Batch 42 — spring harvest”, with the number intact. —

Related guides: How to Batch OCR a Multi-Page PDF with Consistent Settings — How to Add a Searchable Text Layer to a Scanned PDF.

Limits

  • Mode 7 assumes one line: feed it a whole page and it will try to read the page as one line, so crop to the line first.
  • Command-line OCR rewrites the file, so always work on a copy and keep the original scan untouched.
  • UNTESTED — no OCR tool was run; all results above are expected results. The practice file is fictional.

Sources

See all