diff --git a/docs/installation.md b/docs/installation.md index 48f6237..d75cd3d 100644 --- a/docs/installation.md +++ b/docs/installation.md @@ -12,8 +12,11 @@ pip install tetrak The distribution is `tetrak`; the command it installs is `tetrak-ocr`, and the import package is `tetrak_ocr`. A plain install gives you Tesseract (via -`pytesseract`), image handling, PDF rasterisation and the searchable-PDF -writer: enough to process a folder of scans offline. +`pytesseract`), image handling, PDF rasterisation, the searchable-PDF +writer, and the check that every scan passes before any of them opens it +([sillybos](https://github.com/scattercode/sillybos): the file really is a +JPEG, PNG, TIFF or PDF, within size and page limits; anything else goes to +triage with the reason): enough to process a folder of scans offline. Most people want more than that. The install we recommend adds quality scoring, which is what turns on `auto-local`, the backend that runs every @@ -30,9 +33,10 @@ choosing between one engine. Add the extras below to give it a choice. Two things must be installed separately, because they are binaries rather than Python packages. **Tesseract** is the OCR engine itself; `pytesseract` -only wraps it. **Poppler** provides `pdftoppm`, which `pdf2image` shells out -to when rasterising PDF pages. Without poppler, PDF input fails with -`PDFPageCountError`, which does not look like a missing-poppler problem. +only wraps it. **Poppler** provides `pdfinfo`, which the check reads a PDF's +page count and page sizes from, and `pdftoppm`, which `pdf2image` shells out +to when rasterising the pages. Without poppler, the first PDF stops with a +message that says to install it. macOS: diff --git a/pyproject.toml b/pyproject.toml index cc4b6d1..25e8faf 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -46,6 +46,11 @@ dependencies = [ "Pillow>=13.0", "pytesseract>=0.3", "pdf2image>=1.16", + # Decides, before any decoder opens a scan, that it really is a JPEG, PNG, + # TIFF or PDF within our limits (imaging.open_image and check_pdf). Grew + # out of this package; 0.x minor releases are additive, a breaking one + # becomes 1.0 and stops at the ceiling. + "sillybos>=0.1,<1", "python-dotenv>=1.0", # Writes the searchable PDF. Core rather than an extra because the roadmap # treats that as the flagship catalogue-ready output, and it should work on diff --git a/src/tetrak_ocr/imaging.py b/src/tetrak_ocr/imaging.py index a1ab9b9..899bd76 100644 --- a/src/tetrak_ocr/imaging.py +++ b/src/tetrak_ocr/imaging.py @@ -19,12 +19,16 @@ the document. It is also where every file is opened, so it is where files are refused. -:func:`open_image` lets Pillow run only the readers for the formats Tetrak -supports, so a PostScript or FlashPix file renamed ``.png`` never reaches -the readers whose flaws ran commands in October 2026, and refuses an image -with more pixels than any real scan. :func:`check_pdf` does the same for a -PDF's page count and page sizes, from poppler's ``pdfinfo``, before a page is -rendered. See the Tetrak vault's note on checking an image before OCR. +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. +:func:`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; +:func:`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. """ from __future__ import annotations @@ -34,14 +38,12 @@ from contextlib import contextmanager from pathlib import Path +import sillybos from PIL import Image +from sillybos.pillow import pillow_formats from .errors import InputRefusedError, MultiPageNotSupportedError -ALLOWED_FORMATS = ("JPEG", "PNG", "TIFF") -"""The only Pillow readers Tetrak lets run: those for SUPPORTED_EXTENSIONS. -A PDF is never handed to Pillow; poppler renders it.""" - MAX_PIXELS = 89_478_485 """The most pixels in one image or rendered page: Pillow's own warning threshold, and 2.3 times the largest scan Tetrak has read (38.7 million, @@ -57,77 +59,67 @@ """The resolution poppler renders at: pdf2image's default, used everywhere.""" -def _check_pixels(path: Path, image: Image.Image) -> None: - if image.width * image.height > MAX_PIXELS: - raise InputRefusedError( - path, - f"{image.width} x {image.height} is {image.width * image.height:,} pixels; " - f"the limit is {MAX_PIXELS:,}.", - ) +RASTER_FORMATS = frozenset({"jpeg", "png", "tiff"}) +"""What :func:`open_image` accepts: the formats Pillow is allowed to read. A +PDF is never handed to Pillow; poppler renders it.""" + + +def _policy(formats: frozenset[str]) -> sillybos.Policy: + """Tetrak's limits as a sillybos policy, built per call so a test can tune the constants.""" + return sillybos.Policy( + formats=formats, + max_pixels=MAX_PIXELS, + max_pages=MAX_PDF_PAGES, + render_dpi=PDF_RENDER_DPI, + ) + + +def _require(path: Path, formats: frozenset[str]) -> sillybos.Verdict: + """Check *path* against Tetrak's limits; refuse with sillybos's reasons in one sentence.""" + verdict = sillybos.check(path, _policy(formats)) + if not verdict.ok: + raise InputRefusedError(path, "; ".join(verdict.reasons) + ".") + return verdict def open_image(path: Path) -> Image.Image: """Open a raster scan with only Tetrak's readers, refusing an oversized one. - Pillow decides what a file is from its contents, not its name, and then - runs the reader for that format. Passing ``formats`` means only the - JPEG, PNG and TIFF readers can be chosen, whatever the file claims to be. - Only the header has been read when the size is checked; the pixels are + sillybos decides first, from the file's own bytes and header: the format + it really is, its size, and every frame's pixels for a TIFF. Only then is + Pillow asked to open it, with ``formats`` naming the JPEG, PNG and TIFF + readers, so no other reader can be chosen whatever the file claims to + be. Only the header has been read when this returns; the pixels are decoded later, on first use. Raises: - InputRefusedError: If the file is not JPEG, PNG or TIFF, or has more - pixels than :data:`MAX_PIXELS`. + InputRefusedError: If the file is not JPEG, PNG or TIFF, lies about + which it is, or has a frame with more pixels than + :data:`MAX_PIXELS`. """ - try: - image = Image.open(path, formats=ALLOWED_FORMATS) - except Image.UnidentifiedImageError: - raise InputRefusedError( - path, f"It is not one of the formats Tetrak reads ({', '.join(ALLOWED_FORMATS)})." - ) from None - try: - _check_pixels(Path(path), image) - except InputRefusedError: - image.close() - raise - return image + path = Path(path) + policy = _policy(RASTER_FORMATS) + _require(path, RASTER_FORMATS) + return Image.open(path, formats=pillow_formats(policy)) def check_pdf(path: Path) -> int: """Refuse a PDF too long, or with a page too large, to render; else count its pages. - Reads poppler's ``pdfinfo``, which parses the document's structure without - rendering anything. A page's size is in points (1/72 inch), so its pixels - at :data:`PDF_RENDER_DPI` are known before poppler allocates them. + sillybos reads poppler's ``pdfinfo``, which parses the document's + structure without rendering anything, in a separate process with a time + limit. A page's size is in points (1/72 inch), so its pixels at + :data:`PDF_RENDER_DPI` are known before poppler allocates them. Raises: - InputRefusedError: If poppler cannot read the file, it has more pages - than :data:`MAX_PDF_PAGES`, or a page would render to more pixels - than :data:`MAX_PIXELS`. + InputRefusedError: If the file is not a PDF or poppler cannot read + it, it has more pages than :data:`MAX_PDF_PAGES`, a page would + render to more pixels than :data:`MAX_PIXELS`, or it is + encrypted. """ - from pdf2image import pdfinfo_from_path - - try: - pages = int(pdfinfo_from_path(str(path))["Pages"]) - except Exception as exc: - raise InputRefusedError(path, f"poppler could not read it as a PDF ({exc}).") from None - if pages > MAX_PDF_PAGES: - raise InputRefusedError(path, f"It has {pages} pages; the limit is {MAX_PDF_PAGES}.") - info = pdfinfo_from_path(str(path), first_page=1, last_page=pages) - for key, value in info.items(): - # "Page 3 size": "408 x 559.44 pts", with a paper name after on some. - if not (key.startswith("Page") and key.endswith("size")): - continue - width, _, height = str(value).split()[:3] - pixels = (float(width) / 72 * PDF_RENDER_DPI) * (float(height) / 72 * PDF_RENDER_DPI) - if pixels > MAX_PIXELS: - page = key.split()[1] - raise InputRefusedError( - path, - f"Page {page} is {value}, which renders to {pixels:,.0f} pixels at " - f"{PDF_RENDER_DPI} dpi; the limit is {MAX_PIXELS:,}.", - ) - return pages + verdict = _require(Path(path), frozenset({"pdf"})) + assert verdict.facts is not None # a passing verdict always carries its facts + return verdict.facts.pages def pdf_renderer(): @@ -244,8 +236,8 @@ def iter_frames(path: Path) -> Iterator[Image.Image]: for index in range(int(getattr(image, "n_frames", 1))): image.seek(index) if _is_page(image): - # Frame 0 was checked on opening; each later one has its own size. - _check_pixels(path, image) + # Every frame's size was checked by open_image, before Pillow + # opened the file: sillybos walks the whole frame chain. yield image.copy() diff --git a/tests/ocr/test_input_limits.py b/tests/ocr/test_input_limits.py index ff6d30a..87f12c6 100644 --- a/tests/ocr/test_input_limits.py +++ b/tests/ocr/test_input_limits.py @@ -39,7 +39,7 @@ def test_a_file_whose_name_lies_is_refused(self, tmp_path: Path) -> None: path = tmp_path / "postcard.png" page().save(path, format="GIF") - with pytest.raises(InputRefusedError, match="not one of the formats"): + with pytest.raises(InputRefusedError, match="says PNG, is GIF"): open_image(path) def test_a_postscript_file_never_reaches_the_eps_reader(self, tmp_path: Path) -> None: @@ -47,7 +47,7 @@ def test_a_postscript_file_never_reaches_the_eps_reader(self, tmp_path: Path) -> path = tmp_path / "scan.jpg" path.write_bytes(b"%!PS-Adobe-3.0 EPSF-3.0\n%%BoundingBox: 0 0 10 10\n") - with pytest.raises(InputRefusedError, match="not one of the formats"): + with pytest.raises(InputRefusedError, match="says JPEG, is PostScript"): open_image(path) @@ -61,15 +61,17 @@ def test_an_image_over_the_limit_is_refused(self, tmp_path, monkeypatch) -> None open_image(path) def test_a_later_tiff_frame_over_the_limit_is_refused(self, tmp_path, monkeypatch) -> None: - """Each frame has its own size; checking frame 0 alone is not enough.""" + """Each frame has its own size; checking frame 0 alone is not enough. + + The whole chain is walked before Pillow opens the file, so the file + is refused at once, before even the first frame is handed out. + """ monkeypatch.setattr(imaging, "MAX_PIXELS", 200 * 300) path = tmp_path / "register.tif" page().save(path, save_all=True, append_images=[page(400, 600)]) - frames = iter_frames(path) - next(frames).close() - with pytest.raises(InputRefusedError, match="400 x 600"): - next(frames) + with pytest.raises(InputRefusedError, match="240,000 pixels; the limit is 60,000"): + next(iter_frames(path)) @needs_poppler @@ -93,7 +95,7 @@ def test_a_page_too_large_to_render_is_refused_before_rendering(self, tmp_path: path = tmp_path / "poster.pdf" page().save(path, resolution=1) - with pytest.raises(InputRefusedError, match="Page 1 is 14400 x 21600 pts"): + with pytest.raises(InputRefusedError, match="2,400,000,000 pixels"): with streamed_pages(path): pass @@ -101,7 +103,7 @@ def test_a_file_poppler_cannot_read_is_refused(self, tmp_path: Path) -> None: path = tmp_path / "broken.pdf" path.write_bytes(b"not a pdf at all") - with pytest.raises(InputRefusedError, match="could not read it as a PDF"): + with pytest.raises(InputRefusedError, match="unknown format"): check_pdf(path)