Untitled

mail@pastecode.io avatar
unknown
python
2 days ago
1.2 kB
2
Indexable
Never
import fitz  #pip install pymupdf
import os

def extract_images_from_pdf(file_path, output_folder):
    try:
        pdf_document = fitz.open(file_path)
        for page_num in range(len(pdf_document)):
            page = pdf_document[page_num]
            image_list = page.get_images(full=True)
            for image_index, img in enumerate(image_list):
                xref = img[0]
                base_image = pdf_document.extract_image(xref)
                image_bytes = base_image["image"]
                image_ext = base_image["ext"]
                image_filename = f"page{page_num+1}_img{image_index+1}.{image_ext}"
                image_filepath = os.path.join(output_folder, image_filename)
                
                with open(image_filepath, "wb") as image_file:
                    image_file.write(image_bytes)
                print(f"Saved image {image_filename}")
        print("Image extraction complete.")
    except Exception as e:
        print("Error reading file,", e)


file_path = "path/to/your/pdf_file.pdf"
output_folder = "path/to/output/folder"
os.makedirs(output_folder, exist_ok=True)
extract_images_from_pdf(file_path, output_folder)
Leave a Comment