tetrak_ocr.imaging

Frame handling for multi-page raster images.

TIFF is the one raster format in SUPPORTED_EXTENSIONS that can hold more than one page, and archival TIFF frequently does — a scanned pamphlet or register often arrives as a single file with one frame per leaf.

Pillow opens such a file at frame 0 and says nothing about the rest. Every backend here loads images through Pillow, directly or indirectly, so “TIFF support” meant reading page one and silently discarding the others. That is the same failure the Claude backend already guards against with its max_tokens check: a truncated transcript is worse than a failed one, because it looks like a result and quietly corrupts everything downstream of it.

So the rule in this package is that a multi-frame image is either read in full or refused by name. Tesseract reads it in full, page by page, exactly as it already does for PDFs. The backends that cannot yet do so raise MultiPageNotSupportedError, which names the file, the page count and a backend that will read it — rather than returning page one as though that were the document.

It is also where every file is opened, so it is where files are refused. The deciding is done by sillybos (pip install sillybos), which grew out of this module: it names a file’s real format from its first bytes, reads the JPEG, PNG or TIFF header with code of its own, asks pdfinfo about a PDF, and refuses anything outside a policy with a reason in a sentence. open_image() runs that check and then lets Pillow use only the JPEG, PNG and TIFF readers, so a PostScript or FlashPix file renamed .png never reaches the readers whose flaws ran commands in October 2026; check_pdf() does the same for a PDF’s page count and page sizes before a page is rendered. The limits are this module’s constants, set from the corpus; sillybos’s defaults are the same numbers.

Module Attributes

MAX_PIXELS

Pillow's own warning threshold, and 2.3 times the largest scan Tetrak has read (38.7 million, an Armenian page at 600 dpi).

MAX_SIDE

The most pixels along either side of one image, frame, tile or rendered page.

MAX_PDF_PAGES

three times the longest book read so far (166 pages).

PDF_RENDER_DPI

pdf2image's default, used everywhere.

FAINT_INK_LEVEL

A page is faint when even its darkest thousandth of pixels is lighter than this grey level (0 is black).

MIN_INK_SPAN

...and only when that darkest thousandth is at least this far below the paper, so a blank leaf photographed in grey is not stretched into noise.

RASTER_FORMATS

the formats Pillow is allowed to read.

Functions

check_pdf(path)

Refuse a PDF too long, or with a page too large, to render; else count its pages.

detector_input(path)

What an EasyOCR reader is handed for the scan at path.

frame_count(path)

Number of pages in a raster image; 1 for ordinary single-page files.

iter_frames(path)

Yield every page of path as an independent image.

iter_pages(path)

Yield every page of a document, whether it is a PDF or a raster.

lift_faint_ink(image)

Stretch a faint page to full contrast; None for any other page.

open_image(path)

Open a raster scan with only Tetrak's readers, refusing an oversized one.

page_count(path)

Pages in a document of any supported kind, without rendering it.

page_documents(path)

Split path into one single-page document per page, in a temp directory.

pdf_renderer()

Return pdf2image.convert_from_path, or explain why it is missing.

reject_multi_page(path, backend, *[, ...])

Raise if path holds more than one frame.

streamed_pages(path)

Yield page images one at a time, without holding the document in memory.