Repository navigation
fix(ocr): check a scan before any decoder opens it, and stream PDF pages - #86
Merged
Merged
Conversation
Every scan now opens through imaging.open_image, which lets Pillow use only its JPEG, PNG and TIFF readers and refuses an image over MAX_PIXELS, frame by frame for a TIFF. A PDF is checked for page count and page size through pdfinfo before anything renders, and the Tesseract path streams pages one at a time instead of rendering every page into memory first. A refused file raises InputRefusedError with the reason in a sentence. Found on the way: a searchable PDF written from a TIFF with no recorded resolution took Pillow's (1, 1) dpi at face value, so a 200-pixel page became 200 inches wide and rendered to 2.4 billion pixels when read back. A dpi below 50 is now treated as unknown. Co-authored-by: Yvette Mankerian <yvettekm@gmail.com> Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Deploying tetrak with
|
| Latest commit: |
7384a8a
|
| Status: | ✅ Deploy successful! |
| Preview URL: | https://da4b90dc.tetrak.pages.dev |
| Branch Preview URL: | https://fix-image-limits.tetrak.pages.dev |
There was a problem hiding this comment.
🟡 Changes recommended
Critical validation gaps allow refused rasters and over-limit PDFs to reach OCR backends, while calls incompatible with supported pdf2image versions break valid PDF processing.
4 open issues
What changed in this PR
This PR adds shared input checks to the OCR pipeline and reduces Tesseract’s PDF memory use through disk-backed page streaming.
Changes:
- Restricts raster readers and introduces pixel-count, PDF page-count and page-size limits.
- Routes Tesseract PDF processing through
streamed_pagesand adds refusal handling. - Corrects searchable-PDF sizing for TIFFs without meaningful resolution metadata and adds regression tests.
| File | Description |
|---|---|
| tests/ocr/test_ocr_tesseract.py | Adapts PDF mocks for preflight and streaming. |
| tests/ocr/test_multi_page.py | Uses realistic scan dimensions for PDF text tests. |
| tests/ocr/test_input_limits.py | Tests format restrictions, size limits and DPI handling. |
| src/tetrak_ocr/pdf_output.py | Falls back to assumed DPI for implausible metadata. |
| src/tetrak_ocr/orientation.py | Uses guarded image opening and reports refusals as unsure. |
| src/tetrak_ocr/imaging.py | Adds shared input checks and PDF preflight. |
| src/tetrak_ocr/errors.py | Defines InputRefusedError with a refusal reason. |
| src/tetrak_ocr/backends/vision.py | Uses guarded raster opening. |
| src/tetrak_ocr/backends/tesseract.py | Uses guarded raster opening and disk-backed PDF streaming. |
| src/tetrak_ocr/backends/paddle.py | Uses guarded opening for TIFF conversion. |
| src/tetrak_ocr/backends/easyocr.py | Uses guarded opening for TIFF conversion. |
| src/tetrak_ocr/backends/claude.py | Uses guarded opening for TIFF encoding. |
| src/tetrak_ocr/backends/armenian.py | Uses guarded opening for TIFF conversion. |
Review effort: Balanced
| 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) |
| """ | ||
| try: | ||
| with Image.open(path) as image: | ||
| with open_image(path) as image: |
| # down, and each one prints "Cannot close object, library is | ||
| # destroyed. This may cause a memory leak!". A 21-page document | ||
| # opens 22 of them. | ||
| check_pdf(path) |
Comment on lines
+83
to
+84
| image = Image.open(path, formats=ALLOWED_FORMATS) | ||
| except Image.UnidentifiedImageError: |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.


Stage 0 of the vault's "Checking an image before OCR" note, and the "limit how many PDF pages are rendered into memory" item from the security to-do.
imaging.open_image()opens every scan withformats=("JPEG", "PNG", "TIFF"), so a PostScript or FlashPix file renamed.pngnever reaches those readers (the October 2026 Pillow advisories' path), and refuses an image overMAX_PIXELS(Pillow's own bomb threshold, 2.3× the largest scan in the corpus), frame by frame for a TIFF.imaging.check_pdf()reads page count and page sizes frompdfinfobefore rendering;MAX_PDF_PAGESis 500 (3× the longest book so far).streamed_pages, one page at a time.InputRefusedErrorcarries the reason in a sentence;orientation.uprightreports it as "unsure" so the OCR step is where the run hears it.pdf_output: a TIFF with no recorded resolution produced a page 72× too large.518 tests pass locally; 13 xfail as before. New tests in
tests/ocr/test_input_limits.py.🤖 Generated with Claude Code