MCPcopy Create free account
hub / github.com/pymupdf/PyMuPDF / insert_image

Method insert_image

src/__init__.py:12431–12553  ·  view source on GitHub ↗

Insert an image for display in a rectangle. Args: rect: (rect_like) position of image on the page. alpha: (int, optional) set to 0 if image has no transparency. filename: (str, Path, file object) image filename. height: (int) keep_

(
            page,
            rect,
            *,
            alpha=-1,
            filename=None,
            height=0,
            keep_proportion=True,
            mask=None,
            oc=0,
            overlay=True,
            pixmap=None,
            rotate=0,
            stream=None,
            width=0,
            xref=0,
            )

Source from the content-addressed store, hash-verified

12429 return spare_height, scale
12430
12431 def insert_image(
12432 page,
12433 rect,
12434 *,
12435 alpha=-1,
12436 filename=None,
12437 height=0,
12438 keep_proportion=True,
12439 mask=None,
12440 oc=0,
12441 overlay=True,
12442 pixmap=None,
12443 rotate=0,
12444 stream=None,
12445 width=0,
12446 xref=0,
12447 ):
12448 """Insert an image for display in a rectangle.
12449
12450 Args:
12451 rect: (rect_like) position of image on the page.
12452 alpha: (int, optional) set to 0 if image has no transparency.
12453 filename: (str, Path, file object) image filename.
12454 height: (int)
12455 keep_proportion: (bool) keep width / height ratio (default).
12456 mask: (bytes, optional) image consisting of alpha values to use.
12457 oc: (int) xref of OCG or OCMD to declare as Optional Content.
12458 overlay: (bool) put in foreground (default) or background.
12459 pixmap: (pymupdf.Pixmap) use this as image.
12460 rotate: (int) rotate by 0, 90, 180 or 270 degrees.
12461 stream: (bytes) use this as image.
12462 width: (int)
12463 xref: (int) use this as image.
12464
12465 'page' and 'rect' are positional, all other parameters are keywords.
12466
12467 If 'xref' is given, that image is used. Other input options are ignored.
12468 Else, exactly one of pixmap, stream or filename must be given.
12469
12470 'alpha=0' for non-transparent images improves performance significantly.
12471 Affects stream and filename only.
12472
12473 Optimum transparent insertions are possible by using filename / stream in
12474 conjunction with a 'mask' image of alpha values.
12475
12476 Returns:
12477 xref (int) of inserted image. Re-use as argument for multiple insertions.
12478 """
12479 CheckParent(page)
12480 doc = page.parent
12481 if not doc.is_pdf:
12482 raise ValueError("is no PDF")
12483
12484 if xref == 0 and (bool(filename) + bool(stream) + bool(pixmap) != 1):
12485 raise ValueError("xref=0 needs exactly one of filename, pixmap, stream")
12486
12487 if filename:
12488 if type(filename) is str:

Callers 8

replace_imageMethod · 0.80
replace_imageFunction · 0.80
test_insertFunction · 0.80
test_compressFunction · 0.80
test_3087Function · 0.80
test_2348Function · 0.80
test_bboxlogFunction · 0.80
test_barcodeFunction · 0.80

Calls 8

CheckParentFunction · 0.85
FileNotFoundErrorClass · 0.85
RectClass · 0.85
get_page_imagesMethod · 0.80
get_page_xobjectsMethod · 0.80
get_page_fontsMethod · 0.80
wrap_contentsMethod · 0.80
_insert_imageMethod · 0.80

Tested by 6

test_insertFunction · 0.64
test_compressFunction · 0.64
test_3087Function · 0.64
test_2348Function · 0.64
test_bboxlogFunction · 0.64
test_barcodeFunction · 0.64