feat: add image_bbox_to_pdf helper for image-pixel to PDF table_areas#823
Open
mvanhorn wants to merge 1 commit into
Open
feat: add image_bbox_to_pdf helper for image-pixel to PDF table_areas#823mvanhorn wants to merge 1 commit into
mvanhorn wants to merge 1 commit into
Conversation
Collaborator
|
Thanks @mvanhorn — this is a clean, useful helper for the recurring image-pixel↔PDF-coordinate confusion (#377/#780). Reviewed:
CI is fully green. LGTM — nice addition. (Would pair well with a short note in the |
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.
Summary
Users who detect table regions with an external image-based tool (Table Transformers, a pdf2image + detector pipeline, etc.) get bounding boxes in image-pixel space and can now convert them into PDF-space
table_areaswith a single call:camelot.image_bbox_to_pdf(bbox, image_size, pdf_size).Why this matters
Issue #377 asks how to feed image-pixel bounding boxes to
read_pdf(table_areas=...). Camelot already fully supports manual table-area selection; as bosd (COLLABORATOR) confirmed on 2026-05-21, the only missing piece is the coordinate conversion. Image space and PDF space differ in two ways: the origin (image is top-left, PDF is bottom-left) and the units (pixels vs points). Bridging them needs a y-axis flip plus an x/y scale by the page-to-image ratio. There was no public helper for this, and the internalscale_imagerequires a precomputedfactorstuple and is not exposed for this purpose.Changes
camelot/utils.py: new publicimage_bbox_to_pdf(bbox, image_size, pdf_size, as_string=False). Reuses the existingscale/translateprimitives, scales x bypdf_w/img_wand y bypdf_h/img_h, and flips the y-axis so the returned(x1, y1, x2, y2)has bottom-left origin withy1 > y2- exactly whatbbox_from_strandread_pdf(table_areas=...)expect.as_string=Truereturns the"x1,y1,x2,y2"form for direct use as atable_areasentry. Raises a clearValueErroron a zero/negative image or PDF dimension, or a zero-width/zero-height pixel bbox.camelot/__init__.py: export the helper ascamelot.image_bbox_to_pdf.tests/test_utils.py: coverage for the happy path (300 DPI US-Letter), identity scale (y-flip only), string round-trip throughbbox_from_str, top/bottom edge mapping, independent non-square x/y scaling, and the degenerate-input error paths.Testing
pytest tests/test_utils.py-> 18 passedruff checkandruff format --checkon the changed files -> cleanFixes #377