Repository navigation
fix(extract): cut PDF image crops in the displayed page frame - #2845
Conversation
…he page render Object boxes from PDFium are in unrotated user space, but pdf_extraction cropped them from a render that applied the page rotation twice and ignored the crop-box origin. On 90/270-degree pages and on pages whose crop box does not start at (0, 0), crops landed on the wrong region; on 180-degree pages they were upside down. page_rect_to_display maps object boxes into the displayed page (crop box, /Rotate, top-left origin). Crops are cut from the existing page-element render and resized to their size in points, so each page renders once. Signed-off-by: Jacob Ioffe <jioffe@nvidia.com>
Signed-off-by: Jacob Ioffe <jioffe@nvidia.com>
|
I'll fix CI failures and address comments from users with write access. I'll skip comments containing "(aside)".
|
…2-DPI crop render Read the rendered page frame from page.get_bbox() (crop box clipped to the media box, inherited from the page tree) and the rotation once per page instead of get_cropbox() per object, which falls back to 612x792 for inherited boxes and ignores the media box. Crops are cut from main's 72-DPI render again (without the extra rotation), so upright pages with a (0, 0) page box are unchanged byte for byte. Remove the unused convert_pdfium_position and extract_simple_images_from_pdfium_page helpers. Tests check crop and nested-image boxes against FPDF_PageToDevice for inherited, inset and out-of-media page boxes at every rotation. Signed-off-by: Jacob Ioffe <jioffe@nvidia.com>
A top-level image, path or form object that lies entirely outside the displayed page maps to an inverted box after clipping. Keeping it in the grouping input could stretch the crop of a visible neighbour. Drop only inverted boxes so zero-height table rules still join their group. Signed-off-by: Jacob Ioffe <jioffe@nvidia.com>
Update the crop-coordinate fix on current main. Signed-off-by: Jacob Ioffe <jioffe@nvidia.com>
Keep sub-point off-page objects out of crop groups while retaining on-page thin rules. Cover near-edge image placements at every page rotation. Signed-off-by: Jacob Ioffe <jioffe@nvidia.com>
[Medium impact] This PR appears safe to merge. SummaryThis PR aligns PDF image crops with the displayed page, including rotated pages and pages with an offset visible area.
jioffe502 explicitly describes the removed helpers as intentional dead-code cleanup; no remaining repository callers were found. Tests were inspected but not run. Reviews (1) · Last reviewed commit: "fix(extract): reject off-page boxes befo..." · Reviewed by Greptile |
There was a problem hiding this comment.
Note
Newer findings are available below. Devin Review posted a newer report on this PR, in addition to the findings presented here.
Devin Review found 1 finding. Devin's session did not pick them up; it is left for you to triage below.
Crops cut from the page-element render now use #2845's displayed page frame: the render is only resized to points, with no extra rotation. The single-render tests compare against main's scale-1 render and expect a crop on every rotation. Signed-off-by: Jacob Ioffe <jioffe@nvidia.com>
What this fixes
When a PDF page is rotated, or its visible area doesn't start at the page origin, the image crops we cut from it come from the wrong place:
This code came over from nv-ingest (#386, Feb 2025) and has had the bug since then. The fix is in one file (
pdfium.py). Upright pages whose visible area starts at the origin get byte-identical crops, except where an object lies fully off the page (see below).Does this change anything on normal pages? No.
With image extraction on (
extract_images=True, text-only Nemotron 3.5 embed, 4x L40S,mainvs this PR merged onto thatmain):mainruns differ by 0.5% pages/s, so all differences are noise.Stock config (image crops off, same GPU for both arms):
Which pages get different crops (every page, main vs this PR):
2e6ee471, which only drops objects lying less than one point fully off the page (see below).Details for reviewers
Cause.
get_pos()returns boxes in unrotated PDF coordinates with a bottom-left origin.extract_image_like_objects_from_pdfium_pageconverted them using the rotated page size and no page-box origin. It then cropped from arender(rotation=page.get_rotation())image. PDFium already applies the page's/Rotatewhen it renders, so the page was turned twice.get_bbox(), notget_cropbox().get_cropbox()ignores page boxes inherited from the page tree and doesn't clip to the MediaBox, so it isn't the area PDFium actually renders._page_rect_to_display.convert_pdfium_position,extract_simple_images_from_pdfium_page,extract_top_level_simple_images_from_pdfium_page. No other callers in the repo.images[].bbox_xyxy_normis now in the same coordinates as the table, chart and infographic boxes, so image dedup against those elements can start matching there.Scope. Only PDFium image, shape, form and nested-image crop boxes, and they only run when something reads them:
extract_images=True, or auto mode with captioning, dedup, the image store or nested images. Crops are still rendered separately at 72 DPI. Text, tables, charts, infographics, OCR,page_imageandextract.pyare unchanged.Tests.
tests/test_pdf_image_crops.pycovers 5 page-box setups at all 4 rotations, with the image drawn directly or inside a Form XObject:Boxes are checked against PDFium's own
FPDF_PageToDevice. The tests also check that:The coordinate and upright-crop tests fail on
main. Locally: crop and nested-image tests plus the PDF extraction, caption, image-store and graph tests pass (673 passed, 1 skipped), and pre-commit passes.Link to Devin session: https://nvidia-cloud.devinenterprise.com/sessions/1a26768094554997905f04cfc7e6a53a
Open in Devin Desktop: https://nvidia-cloud.devinenterprise.com/desktop/session/1a26768094554997905f04cfc7e6a53a?variant=devin
Requested by: @jioffe502
🟢 Devin Review found nothing to fix · View findings · Follow Devin's work