OiO.lk Blog pdf Python, replacing images in a PDF with Fitz
pdf

Python, replacing images in a PDF with Fitz


I have the following code to replace images in a PDF. This mostly works, but for some reason both images are being replaced with the same image and not 2 different images. I have tried isolating the code to do just one image and still both images seem to be being replaced with the same image

import fitz 
import os

# Set the directories
pdf_dir = r"path"
image_dir_macro = r"path"
image_dir_micro = r"path"
output_dir = r"path"

# Function to replace the second image on the first page with the macro map
def replace_macro_map(pdf, macro_path):
    page1 = pdf[0]
    image_list_1 = page1.get_images(full=True)
    if len(image_list_1) > 1:
        subject_xref = image_list_1[1][0]
        page1.replace_image(xref=subject_xref, filename=macro_path)
    else:
        print(f"Less than 2 images on page 1.")

# Function to replace the first image on the second page with the micro map
def replace_micro_map(pdf, micro_path):
    page2 = pdf[1]
    image_list_2 = page2.get_images(full=True)
    if len(image_list_2) > 0:
        comparable_xref = image_list_2[0][0]
        page2.replace_image(xref=comparable_xref, filename=micro_path)
    else:
        print(f"No images found on page 2.")

# Loop through the PDF files in the directory
for pdf_file in os.listdir(pdf_dir):
    try:
        # Define paths for macro and micro maps
        macro_path = os.path.join(image_dir_macro, pdf_file.replace(".pdf", " - Macro Map.png"))
        micro_path = os.path.join(image_dir_micro, pdf_file.replace(".pdf", " - Micro Map.jpg"))

        # Input and output PDF paths
        input_file = os.path.join(pdf_dir, pdf_file)
        output_file = os.path.join(output_dir, pdf_file)

        # Open the PDF
        pdf = fitz.open(input_file)

        # Replace images in isolation
        replace_macro_map(pdf, macro_path)
        replace_micro_map(pdf, micro_path)

        # Save the modified PDF
        pdf.save(output_file)
        pdf.close()

    except Exception as e:
        print(f"Failed on: {pdf_file}")
        print(f"Error: {e}")



You need to sign in to view this answers

Exit mobile version