Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ability to group collections of image bounding boxes #386

Merged
merged 15 commits into from
Feb 27, 2025

Conversation

edknv
Copy link
Collaborator

@edknv edknv commented Jan 29, 2025

Description

Partially addresses #353. This PR is focused on PDFs. Other document types will be addressed in future PRs.

This PR adds support for a new extract_images_method option in the PDFium-based extraction workflow, allowing users to choose between a simple “as-is” extraction of image objects (simple) or a “grouping” approach (group) that groups related bounding boxes together. It introduces a new parameter, extract_images_params, for configuring thresholds and additional clustering details.

In the previous approach (simple), we only extracted PdfImageObjects, but PDFium provides several additional graphics objects in addition to PdfImageObject. For our purposes, we now also extract:

  • The PdfPathObject FPDF_PAGEOBJ_PATH The PdfPathObject is an arbitrary shape made up of straight lines, rectangles, and cubic Bézier curves. A path may intersect itself and may have disconnected sections and holes.
    The PdfFormObject FPDF_PAGEOBJ_FORM is used to group graphical elements together as a unit for various purposes.

As an example, the following has a PdfFormObject. The object inside the red box is a form object that contains several nested component images. If we use the bounding box of the PdfFormObject (instead of naively extracting all the images with depth > 1), we can correctly group together all the components.
image

In the following example, we have what looks like an image, but it's actually made of many "shapes" (PdfPathObject). We can group them together as one single object.
image

Checklist

  • I am familiar with the Contributing Guidelines.
  • New or existing tests cover these changes.
  • The documentation is up to date with these changes.

@edknv edknv force-pushed the edwardk/pdfium-form-images branch from 704f5bf to 2882d4b Compare January 30, 2025 06:09
@edknv edknv marked this pull request as ready for review February 5, 2025 03:51
@edknv edknv requested a review from drobison00 February 5, 2025 03:51
@mara004
Copy link

mara004 commented Feb 6, 2025

Note that the docs linked above are for a commercial .NET SDK and not applicable to pypdfium2/pdfium.
The official docs are here:
https://pypdfium2.readthedocs.io/
https://pdfium.googlesource.com/pdfium/+/refs/heads/main/public/ (see the comments in the header files)

"""
Convert a PDFium bounding box (which typically has an origin at the bottom-left)
to a more standard bounding-box format with y=0 at the top.
"""
Copy link

Choose a reason for hiding this comment

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

I'm not sure this method is generally correct, as the PDF spec allows for a pretty arbitrary coordinate system.
Indeed, the origin typically is the bottom left, but IIRC the coordinate system can be laid out between any opposite corners in principle. Also, the coordinate system does not necessarily have to start at 0, it could be any arbitrary value.

Related: pypdfium2-team/pypdfium2#284

@edknv edknv requested a review from a team as a code owner February 27, 2025 05:12
@edknv edknv requested review from jdye64 and removed request for a team February 27, 2025 05:12
@edknv edknv merged commit aa015bc into NVIDIA:main Feb 27, 2025
2 checks passed
@edknv edknv deleted the edwardk/pdfium-form-images branch February 27, 2025 05:23
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.

3 participants