Skip to content

pymupdf.Page.replace_image() will save an image stream file 2 times #4327

Description

@Muolae

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.

Image

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions