-
Notifications
You must be signed in to change notification settings - Fork 678
Closed
Description
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
Ideal extracted images
Could you please guide me on how to modify the code to achieve this?
Thank you for your assistance!
Metadata
Metadata
Assignees
Labels
No labels

