[Python] How can I extract figures/images with their captions from textbook PDFs or scanned...

Discussão em 'Python' iniciado por Stack, Setembro 3, 2025.

  1. Stack

    Stack Membro Participativo

    I’m working on a project where I need to extract figures and their respective captions from PDF textbooks. There are direct pages and also scanned pages.

    What I’ve tried so far:

    page.get_images() → works only if the PDF has embedded raster images (many of mine are vector diagrams, so this fails).

    Rendering the whole page (page.get_pixmap) → works, but gives me full-page screenshots, not just the figure.

    Cropping above captions detected via regex (Figure \d+.\d+) → sometimes works, but often captures too much (like unrelated sections).

    Sometimes images generated are blank

    The main challenge: Many textbook figures are vector drawings + text (not stored as standalone images). I need a way to reliably associate a caption like “Figure 1.1” with its corresponding figure and crop only that part of the page.

    Tech stack I’m using:

    Python

    PyMuPDF (fitz)

    PIL + pytesseract (OCR fallback if needed)

    What I need:

    Extract only the figure image/diagram and its caption (e.g., “Figure 1.10 Oxidation of copper to copper oxide”).

    Save output in a structured format like:

    { "page": 12, "caption": "Figure 1.10 Oxidation of copper to copper oxide", "image_file": "page_12_figure1.png" }

    Has anyone solved this problem before, or can suggest a robust approach/library for extracting figures with captions from PDFs?

    Note:I am also attaching a sample images generated for the reference. [​IMG]

    Continue reading...

Compartilhe esta Página