Skip to content

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

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

How to extract not original images but cropped images #3967

Closed
yuki-tashiro opened this issue Oct 19, 2024 · 1 comment
Closed

How to extract not original images but cropped images #3967

yuki-tashiro opened this issue Oct 19, 2024 · 1 comment

Comments

@yuki-tashiro
Copy link

Hello,

Thank you for your great work on this project!

I would like to know how I can extract the cropped portion of images in a PDF, instead of the original, uncropped images.

Currently, I'm able to extract the original images using page.get_image_bbox(), but this doesn't account for images that are cropped on the page. What I need is to extract only the part of the image that is visible on the PDF page, excluding the portions that may have been cropped out.

Here’s my code along with the input PDF and the output images:

import fitz  # PyMuPDF
from PIL import Image
import io

def extract_visible_images_from_pdf(pdf_path, output_folder):

    pdf_document = fitz.open(pdf_path)

    # Process each page
    for page_num in range(len(pdf_document)):
        page = pdf_document.load_page(page_num)
        
        # Render the entire page as an image
        pix = page.get_pixmap()
        page_image = Image.frombytes("RGB", [pix.width, pix.height], pix.samples)

        image_list = page.get_images(full=True)
        print(f"image_list: {image_list}")

        # Get all images on the page
        if image_list:
            for img_index, img_info in enumerate(image_list):
                xref = img_info[0]
                base_image = pdf_document.extract_image(xref)
                image_stream = io.BytesIO(base_image["image"])
                img = Image.open(image_stream)

                # Extract the image bytes from the PDF
                image_rect = page.get_image_bbox(img_info)
                visible_image = page_image.crop((
                    int(image_rect.x0),
                    int(image_rect.y0),
                    int(image_rect.x1),
                    int(image_rect.y1)
                ))

                img_output_path = f"{output_folder}/page_{page_num + 1}_image_{img_index + 1}.png"
                
                visible_image.save(img_output_path)
                print(f"Saved cropped image: {img_output_path}")
        else:
            print(f"No images found on page {page_num + 1}")

    pdf_document.close()

# pdf_path = "/content/drive/MyDrive/MatsuoLab/LLaVA-Med-JP/tp240424-01d_02.pdf"
pdf_path = "/content/input.pdf"
output_folder = "/content/"
extract_visible_images_from_pdf(pdf_path, output_folder)

Here is my original pdf.

Extrated images

  • page_1_image_1
  • page_1_image_2

Ideal extracted images

  • cropped_image

Could you please guide me on how to modify the code to achieve this?

Thank you for your assistance!

@yuki-tashiro yuki-tashiro changed the title how to extract not original images but cropped images How to extract not original images but cropped images Oct 19, 2024
@JorjMcKie
Copy link
Collaborator

This is no issue, but a typical Discussions item ... transferring.

@pymupdf pymupdf locked and limited conversation to collaborators Oct 19, 2024
@JorjMcKie JorjMcKie converted this issue into discussion #3969 Oct 19, 2024

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants