Skip to content

fix(extract): cut PDF image crops in the displayed page frame - #2845

Merged
jioffe502 merged 6 commits into
mainfrom
jioffe/pdf-crop-rotation
Nov 24, 2026
Merged

jioffe502 merged 6 commits into
mainfrom
jioffe/pdf-crop-rotation

Conversation

@jioffe502

@jioffe502 jioffe502 commented Nov 10, 2026 •

Copy link
Copy Markdown
Collaborator

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:

  • Sideways pages (90° / 270°): the crop is of the wrong region, often blank.
  • Upside-down pages (180°): the crop is upside down.
  • Pages whose visible area is offset: the crop is shifted off the image.

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, main vs this PR merged onto that main):

Workload pages/s main / PR Recall@10 main / PR GPU util main / PR Extracted crops main / PR
bo767 71.17, 71.51 (2 runs) / 71.28 0.9233, 0.9243 / 0.9233 76.1% / 76.6% 58,896 / 59,049
ViDoRe v3 English (5-set avg) 19.25 / 19.42 0.6312 / 0.6317 19.2% / 20.1% 13,313 / 13,381
bo10k 80.32 / 80.60 0.7535 / 0.7543 67.5% / 68.8% 291,738 / 292,629
  • The two bo767 main runs differ by 0.5% pages/s, so all differences are noise.
  • With default settings crops aren't embedded (no captioning), so these runs show no throughput or recall regression, not crop correctness. The tests below cover correctness.

Stock config (image crops off, same GPU for both arms):

Dataset Recall@5 main / PR Recall@10 main / PR nDCG@10 main / PR pages/s main / PR
ViDoRe v3 computer_science 0.636 / 0.636 0.742 / 0.742 0.726 / 0.725 19.7 / 19.6
ViDoRe v3 energy 0.642 / 0.641 0.727 / 0.728 0.642 / 0.642 28.5 / 28.3
ViDoRe v3 finance_en 0.549 / 0.550 0.643 / 0.644 0.587 / 0.589 32.4 / 32.0
ViDoRe v3 finance_fr 0.407 / 0.405 0.511 / 0.512 0.431 / 0.431 19.6 / 19.8
ViDoRe v3 hr 0.525 / 0.526 0.625 / 0.627 0.596 / 0.597 15.4 / 15.7
ViDoRe v3 industrial 0.425 / 0.424 0.502 / 0.502 0.471 / 0.470 40.6 / 40.2
ViDoRe v3 pharmaceuticals 0.552 / 0.555 0.645 / 0.646 0.608 / 0.610 31.9 / 31.9
ViDoRe v3 physics 0.366 / 0.366 0.470 / 0.469 0.454 / 0.453 24.2 / 24.1
bo767 0.894 / 0.893 0.925 / 0.926 0.786 / 0.784 40.8 / 40.7
dc10k 0.719 / 0.720 0.752 / 0.754 0.625 / 0.628 47.7 / 47.8
  • All differences are within ±0.006, which is run-to-run noise. The default config doesn't cut image crops at all, so these runs are identical by construction.

Which pages get different crops (every page, main vs this PR):

Dataset Pages Rotated Visible area not at origin Pages whose crops change Crops main / PR
ViDoRe v3 computer_science 1,360 0 0 0 1,673 / 1,673
ViDoRe v3 energy 2,225 0 123 123 3,150 / 3,148
ViDoRe v3 finance_en 2,942 0 0 0 2,542 / 2,542
ViDoRe v3 finance_fr 2,384 1 0 1 2,634 / 2,634
ViDoRe v3 hr 1,110 0 0 0 1,014 / 1,014
ViDoRe v3 industrial 5,244 12 2,787 1,628 6,016 / 6,084
ViDoRe v3 pharmaceuticals 2,313 0 2 1 2,244 / 2,244
ViDoRe v3 physics 1,674 0 0 0 4,407 / 4,407
bo767 54,730 2,630 2,488 3,285 61,500 / 61,653
total 73,982 2,643 5,400 5,038 85,180 / 85,399
  • Only 3 of the 65,986 upright pages with the visible area at the origin change. Each has an object lying fully off the page (see below). Every other such page has byte-identical crops.
  • Crop counts include nested images. No errors in either version.
  • CPU time per page is the same: median 18.4 vs 18.6 ms, p99 222.5 vs 221.9 ms.
  • The numbers above were measured before 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_page converted them using the rotated page size and no page-box origin. It then cropped from a render(rotation=page.get_rotation()) image. PDFium already applies the page's /Rotate when it renders, so the page was turned twice.

# before
bbox = convert_pdfium_position(obj.get_pos(), page.get_width(), page.get_height())
crop = crop_image(render(scale=1, rotation=page.get_rotation()), bbox)
# after
page_box, rotation = page.get_bbox(), page.get_rotation()
bbox = _page_rect_to_display(obj.get_pos(), page_box, rotation)
crop = crop_image(render(scale=1), bbox)
  • get_bbox(), not get_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.
  • One mapping for all crop types. Image, shape, form and nested-image boxes all go through _page_rect_to_display.
  • Off-page objects are dropped before boxes are grouped, including ones less than one point past the edge that would otherwise round to a zero-height box. Before, an object lying fully off the page could stretch the crop of a visible neighbour. Zero-height boxes that intersect the page, such as thin table rules, are kept.
  • Dead code removed: convert_pdfium_position, extract_simple_images_from_pdfium_page, extract_top_level_simple_images_from_pdfium_page. No other callers in the repo.
  • Dedup. On rotated and offset pages, images[].bbox_xyxy_norm is 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_image and extract.py are unchanged.

Tests. tests/test_pdf_image_crops.py covers 5 page-box setups at all 4 rotations, with the image drawn directly or inside a Form XObject:

  • a standard page box, an offset one, and one inherited from the page tree;
  • an inset CropBox inherited from the page tree;
  • a CropBox outside the MediaBox.

Boxes are checked against PDFium's own FPDF_PageToDevice. The tests also check that:

  • crops land on the image and are upright as displayed;
  • an off-page object, including one less than one point past the edge, doesn't widen a visible crop;
  • thin table rules stay in their shape group.

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

Devin Review

…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>
@devin-ai-integration

Copy link
Copy Markdown
Contributor

I'll fix CI failures and address comments from users with write access. I'll skip comments containing "(aside)".

  • Disable automatic comment, CI, and merge conflict monitoring

…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>
@devin-ai-integration
devin-ai-integration Bot marked this pull request as ready for review November 17, 2026 14:47
@devin-ai-integration
devin-ai-integration Bot requested review from a team as code owners November 17, 2026 14:47
@greptile-apps

greptile-apps Bot commented Nov 17, 2026 •

Copy link
Copy Markdown
Contributor

RetriggerView in GreptileConfidence Score: 5/5

[Medium impact] This PR appears safe to merge.

Summary

This PR aligns PDF image crops with the displayed page, including rotated pages and pages with an offset visible area.

  • PDF crops now follow the page as PDFium displays it.

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

@devin-ai-integration devin-ai-integration Bot left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

View findings in Devin Review · Follow Devin's work

Devin Review

@devin-ai-integration devin-ai-integration Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✅ Devin Review: No Issues Found

Devin Review analyzed this PR and found no bugs or issues to report.

Devin Review

@jioffe502
jioffe502 merged commit 6598679 into main Nov 24, 2026
14 checks passed
jioffe502 added a commit that referenced this pull request Nov 24, 2026
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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants