MCPcopy Index your code
hub / github.com/pymupdf/PyMuPDF / replace_image

Method replace_image

src/__init__.py:12818–12854  ·  view source on GitHub ↗

Replace the image referred to by xref. Replace the image by changing the object definition stored under xref. This will leave the pages appearance instructions intact, so the new image is being displayed with the same bbox, rotation etc. By providing a small fully tr

(
            page: 'Page',
            xref: int,
            *,
            filename=None,
            pixmap=None,
            stream=None,
            )

Source from the content-addressed store, hash-verified

12816 self.this = page
12817
12818 def replace_image(
12819 page: 'Page',
12820 xref: int,
12821 *,
12822 filename=None,
12823 pixmap=None,
12824 stream=None,
12825 ):
12826 """Replace the image referred to by xref.
12827
12828 Replace the image by changing the object definition stored under xref. This
12829 will leave the pages appearance instructions intact, so the new image is
12830 being displayed with the same bbox, rotation etc.
12831 By providing a small fully transparent image, an effect as if the image had
12832 been deleted can be achieved.
12833 A typical use may include replacing large images by a smaller version,
12834 e.g. with a lower resolution or graylevel instead of colored.
12835
12836 Args:
12837 xref: the xref of the image to replace.
12838 filename, pixmap, stream: exactly one of these must be provided. The
12839 meaning being the same as in Page.insert_image.
12840 """
12841 doc = page.parent # the owning document
12842 if not doc.xref_is_image(xref):
12843 raise ValueError("xref not an image") # insert new image anywhere in page
12844 if bool(filename) + bool(stream) + bool(pixmap) != 1:
12845 raise ValueError("Exactly one of filename/stream/pixmap must be given")
12846 new_xref = page.insert_image(
12847 page.rect, filename=filename, stream=stream, pixmap=pixmap
12848 )
12849 doc.xref_copy(new_xref, xref) # copy over new to old
12850 last_contents_xref = page.get_contents()[-1]
12851 # new image insertion has created a new /Contents source,
12852 # which we will set to spaces now
12853 doc.update_stream(last_contents_xref, b" ")
12854 page._image_info = None # clear cache of extracted image information
12855
12856 @property
12857 def rotation(self):

Callers 2

delete_imageMethod · 0.80
delete_imageFunction · 0.80

Calls 5

xref_is_imageMethod · 0.80
insert_imageMethod · 0.80
xref_copyMethod · 0.80
get_contentsMethod · 0.80
update_streamMethod · 0.80

Tested by

no test coverage detected