Description of the bug
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.
PyMuPDF version
1.25.2
Operating system
Windows
Python version
3.9
Description of the bug
new_xref = page.insert_image( page.rect, filename=filename, stream=stream, pixmap=pixmap ) doc.xref_copy(new_xref, xref) # copy over new to oldthe 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.
PyMuPDF version
1.25.2
Operating system
Windows
Python version
3.9