Skip to content

PyMuPDF delete only specific image same as 1.png from a pdf pages #2707

@BeastHunterTest1

Description

@BeastHunterTest1

hi i tried some code using chat gtb to delete all images same as 1.png from pdf called 1.pdf this is the code

`import fitz # PyMuPDF

def remove_image_from_pdf(input_pdf, output_pdf, image_name):
# Open the input PDF file
pdf_document = fitz.open(input_pdf)

for page_number in range(len(pdf_document)):
    page = pdf_document[page_number]
    xref_list = page.get_images(full=True)
    
    for xref in xref_list:
        base_image = pdf_document.extract_image(xref[0])
        image_data = base_image["image"]

        if image_data.endswith(b".png") and image_data.get("name") == image_name.encode():
            # Remove the image from the page
            page.apply_redact([xref])
            page.insert_redact(xref, width=0)
            page.apply_redactions()

# Save the modified PDF to the output file
pdf_document.save(output_pdf)
pdf_document.close()

if name == "main":
input_pdf_file = "1.pdf"
output_pdf_file = "1_remove.pdf"
image_name_to_remove = "1.png"

remove_image_from_pdf(input_pdf_file, output_pdf_file, image_name_to_remove)
print(f"Image '{image_name_to_remove}' removed and saved as '{output_pdf_file}'.")

`

and it...does save the new pdf but doesnot remove anything sooo i am trying to remove a translate img from pdf btw.
any help will be great. thanksss.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions