You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
pymupdf.Page.replace_image() will save an image stream file 2 times #4327
new_xref = page.insert_image( page.rect, filename=filename, stream=stream, pixmap=pixmap ) doc.xref_copy(new_xref, xref) # copy over new to old
the code doc.xref_copy(new_xref, xref)copy the stream of new_image to the old xref,but it do not delete the stream of new_image. this bug will result in the old xref and the new_xref Point to the same data, and the same data is stored 2 times in the pdf file.
I want to replace the original picture with a smaller one.However, I found that the file size after replacement is more than the file size obtained by directly deleting the image plus the volume of the replaced image, which is exactly equal to the size of the image file used to replace, so I guess the file is actually stored twice.I looked at the source code and came to the conclusion above.
I add page.delete_image(new_xref)
after it can Fix this,but I think it has batter solution.
How to reproduce the bug
use pymupdf.Page.replace_image to change image size,then look the change of size of pdf file.
sorry my English is bad.
I don't understand what you are actually doing: please provide your PDF and your script.
We cannot deal with issues that do not follow the requirements as stated in the bug issue form.
One intermediate comment though:
When saving the PDF after replacing / deleting an image make sure to use garbage collection and compression options so that the old data is removed! Recommended is using doc.ez_save().
I want to replace the original image in the pdf file with a smaller image, my program extracts each image in the file, then uses a compression function to reduce their size, and then replaces the original image. But I found that for example, if I delete all the pictures directly, the resulting file size is 1MB, and if I replace the original picture with a total size of 2MB, I get a file with a size of 5MB. I think the reasonable size is 3MB.This means that these pictures store two copies of the same data in a pdf file.Thank you for your reply
` for page in doc.pages():
imageList = page.get_images()
Activity
JorjMcKie commentedon Feb 25, 2025
I don't understand what you are actually doing: please provide your PDF and your script.
We cannot deal with issues that do not follow the requirements as stated in the bug issue form.
One intermediate comment though:
When saving the PDF after replacing / deleting an image make sure to use garbage collection and compression options so that the old data is removed! Recommended is using
doc.ez_save().Muolae commentedon Feb 25, 2025
I want to replace the original image in the pdf file with a smaller image, my program extracts each image in the file, then uses a compression function to reduce their size, and then replaces the original image. But I found that for example, if I delete all the pictures directly, the resulting file size is 1MB, and if I replace the original picture with a total size of 2MB, I get a file with a size of 5MB. I think the reasonable size is 3MB.This means that these pictures store two copies of the same data in a pdf file.Thank you for your reply
` for page in doc.pages():
imageList = page.get_images()
JorjMcKie commentedon Feb 25, 2025
I see no issue here, but rather a request for help. Moving this to "Discussions".