DEV Community

A scanned PDF page can be three images: OCR on the background layer read 186 characters

A script that extracts "the image" from a scanned PDF and sends it to OCR assumes the page is one image. On one public-domain page in this test it was three, and the OCR result depended on which of the three got picked: 186 characters at a 93.62% character error rate for one, zero characters for another, 0.33% for the third.

Rendering the whole page and recognizing that gave 0.26%. The numbers below come from a single run on one Apple-silicon Mac, on one page.

What the page is made of

The page is page 16 of the 1921 edition of Psycho-Analysis and the War Neuroses (Ferenczi, Abraham, Simmel, Jones, preface by Freud), held by the Internet Archive under Public Domain Mark 1.0. The reference text was checked by hand against the page image by one reader, so treat the percentages as rough.

This file stores the page image as three layers: a low-resolution background, a foreground colour layer and a 1-bit mask.

Layer Encoding Size
Background JPEG 2000 755 × 1163
Foreground colour JPEG 2000 2265 × 3489
Mask JBIG2, 1 bit 2265 × 3489

pdfimages -all exported two .jp2 files and one .jb2e file, without decoding them. pdfimages -png decoded all three. On top of the images the archive had added an invisible text layer of 2,710 characters.

What each layer gave when recognized alone

Tesseract 5.5 with the English pack ran on each layer after it was decoded to PNG, and then on the full page rendered to an image.

Input Result
Background layer only 186 characters recognized, 93.62% error
Foreground colour layer 0 characters
Mask layer (JBIG2) 9 errors, 0.33%
Full page, rendered 7 errors, 0.26%

The 186 characters and the 93.62% are the result for the background image alone, not for the page. That layer holds the paper tone with the writing wiped out, so Tesseract returned scraps of text and no error message. Nothing about the call looked wrong.

The mask is the opposite case: the shapes of the letters live there, and it scored almost as well as the composited page. Rendering worked with three tools here: pdftoppm, Ghostscript and PyMuPDF each produced a normal page, which is what a reader sees.

A second page that behaves differently

The other sample was half a page from the 1986 Simplified Character Table (National Language Commission), which the Internet Archive also lists under Public Domain Mark 1.0. Its layers are arranged differently: its first image already carries the text, and recognizing that image alone returned 1,759 characters.

So "take the first image" would have looked fine on this file and failed on the English one. Two pages say little about scans in general.

Measured, and what remains a guess

Measured in this run: the layer sizes and encodings above, the four recognition results, the export and decode behaviour of pdfimages, and the fact that three renderers produced a normal page.

Not measured, only inferred: why the file was built this way. A guess is that the sharp glyph shapes sit in the mask and the background is kept small, which fits the sizes in the table (755 by 1163 against 2265 by 3489), but nothing here inspected how the file was produced.

The same goes for how common this layout is. Only two files were opened, so it could be rare or it could be the default for an entire archive.

One more inference concerns pipelines that grab the largest image. On this page the foreground colour layer and the mask share the same pixel size, so "largest" does not pick a unique layer, and which one wins would depend on the tool. That was not tried.

Where ImgIng fits

One tool that puts a text layer and recognized text in the same output is ImgIng, at https://imging.ai/. On a desktop browser its PDF content extraction merges the native text with text recognized inside images and labels the second part as an "Image OCR" block.

A scan whose page images are JPEG 2000 or JBIG2, like the 1921 page, is currently not recognized by it, so such a file goes through rendering to an ordinary image first.

What to do with this

If a script has to feed scanned PDFs to OCR, render each page at a fixed resolution and recognize the render, instead of picking an embedded image. Then check the character count of the result against the page: a page that returns a couple of hundred characters from a full page of print is the signal that the wrong layer, or the wrong step, got used.

Whether the text layer already in the file is better than a new recognition is a separate question, and on these two pages the answer went in opposite directions.

Read on DEV Community ↗ ← Back to News

Comments

No comments yet. Start the discussion.