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

Function insert_image

src_classic/utils.py:291–423  ·  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. keep_proportion: (bool) keep width / height ratio (

(page, rect, **kwargs)

Source from the content-addressed store, hash-verified

289
290
291def insert_image(page, rect, **kwargs):
292 """Insert an image for display in a rectangle.
293
294 Args:
295 rect: (rect_like) position of image on the page.
296 alpha: (int, optional) set to 0 if image has no transparency.
297 filename: (str, Path, file object) image filename.
298 keep_proportion: (bool) keep width / height ratio (default).
299 mask: (bytes, optional) image consisting of alpha values to use.
300 oc: (int) xref of OCG or OCMD to declare as Optional Content.
301 overlay: (bool) put in foreground (default) or background.
302 pixmap: (Pixmap) use this as image.
303 rotate: (int) rotate by 0, 90, 180 or 270 degrees.
304 stream: (bytes) use this as image.
305 xref: (int) use this as image.
306
307 'page' and 'rect' are positional, all other parameters are keywords.
308
309 If 'xref' is given, that image is used. Other input options are ignored.
310 Else, exactly one of pixmap, stream or filename must be given.
311
312 'alpha=0' for non-transparent images improves performance significantly.
313 Affects stream and filename only.
314
315 Optimum transparent insertions are possible by using filename / stream in
316 conjunction with a 'mask' image of alpha values.
317
318 Returns:
319 xref (int) of inserted image. Re-use as argument for multiple insertions.
320 """
321 CheckParent(page)
322 doc = page.parent
323 if not doc.is_pdf:
324 raise ValueError("is no PDF")
325
326 valid_keys = {
327 "alpha",
328 "filename",
329 "height",
330 "keep_proportion",
331 "mask",
332 "oc",
333 "overlay",
334 "pixmap",
335 "rotate",
336 "stream",
337 "width",
338 "xref",
339 }
340 s = set(kwargs.keys()).difference(valid_keys)
341 if s != set():
342 raise ValueError(f"bad key argument(s): {s}.")
343 filename = kwargs.get("filename")
344 pixmap = kwargs.get("pixmap")
345 stream = kwargs.get("stream")
346 mask = kwargs.get("mask")
347 rotate = int(kwargs.get("rotate", 0))
348 width = int(kwargs.get("width", 0))

Callers

nothing calls this directly

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
_insert_imageMethod · 0.80
getMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…