PyMuPDF delete only specific image same as 1.png from a pdf pages #2708
-
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):
if name == "main":
` and it...does save the new pdf but doesnot remove anything sooo i am trying to remove a translate img from pdf btw. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
This a typical "Discussions" item, so I am going to move it there first. |
Beta Was this translation helpful? Give feedback.
In PDF, images have no name! So you cannot delete them in this way.
I recommend the following approach:
For a given image, compute the hash code (like MD5) from its binary content.
Then loop over the images and extract them via
doc.extract_image(xref)
as you did. For each image extracted in this way, compute the hash code fromimg["image"]
and compare it with that of the file.If equal, delete the image as indicated.
A lot of things may still go wrong - which is the reason why we won't make an official enhancement - e.g.: