def insertImage(page, rect, filename=None, pixmap=None, stream=None, rotate=<NUM_LIT:0>,<EOL>keep_proportion = True,<EOL>overlay=True):
def calc_matrix(fw, fh, tr, rotate=<NUM_LIT:0>):<EOL><INDENT>"""<STR_LIT>"""<EOL>tmp = Point((tr.x1 + tr.x0) / <NUM_LIT>, (tr.y1 + tr.y0) / <NUM_LIT>)<EOL>rot = Matrix(rotate)  <EOL>m = Matrix(<NUM_LIT:1>, <NUM_LIT:0>, <NUM_LIT:0>, <NUM_LIT:1>, -<NUM_LIT:0.5>, -<NUM_LIT:0.5>) * rot<EOL>small = min(fw, fh)  <EOL>if rotate not in (<NUM_LIT:0>, <NUM_LIT>):<EOL><INDENT>fw, fh = fh, fw  <EOL><DEDENT>if fw < <NUM_LIT:1>: <EOL><INDENT>if (float(tr.width) / fw) > (float(tr.height) / fh):<EOL><INDENT>w = tr.height * small<EOL>h = tr.height<EOL><DEDENT>else:<EOL><INDENT>w = tr.width<EOL>h = tr.width / small<EOL><DEDENT><DEDENT>elif fw != fh:  <EOL><INDENT>if (float(tr.width) / fw) > (float(tr.height) / fh):<EOL><INDENT>w = tr.height / small<EOL>h = tr.height<EOL><DEDENT>else:<EOL><INDENT>w = tr.width<EOL>h = tr.width * small<EOL><DEDENT><DEDENT>else: <EOL><INDENT>w = tr.width<EOL>h = tr.height<EOL><DEDENT>m *= Matrix(w, h)  <EOL>m *= Matrix(<NUM_LIT:1>, <NUM_LIT:0>, <NUM_LIT:0>, <NUM_LIT:1>, tmp.x, tmp.y)  <EOL>return m<EOL><DEDENT>CheckParent(page)<EOL>doc = page.parent<EOL>if not doc.isPDF:<EOL><INDENT>raise ValueError("<STR_LIT>")<EOL><DEDENT>if bool(filename) + bool(stream) + bool(pixmap) != <NUM_LIT:1>:<EOL><INDENT>raise ValueError("<STR_LIT>")<EOL><DEDENT>if filename and not os.path.exists(filename):<EOL><INDENT>raise FileNotFoundError("<STR_LIT>" % filename)<EOL><DEDENT>elif stream and type(stream) not in (bytes, bytearray, io.BytesIO):<EOL><INDENT>raise ValueError("<STR_LIT>")<EOL><DEDENT>elif pixmap and type(pixmap) is not Pixmap:<EOL><INDENT>raise ValueError("<STR_LIT>")<EOL><DEDENT>while rotate < <NUM_LIT:0>:<EOL><INDENT>rotate += <NUM_LIT><EOL><DEDENT>while rotate > <NUM_LIT>:<EOL><INDENT>rotate -= <NUM_LIT><EOL><DEDENT>if rotate not in (<NUM_LIT:0>, <NUM_LIT>, <NUM_LIT>, <NUM_LIT>):<EOL><INDENT>raise ValueError("<STR_LIT>")<EOL><DEDENT>r = page.rect & rect<EOL>if r.isEmpty or r.isInfinite:<EOL><INDENT>raise ValueError("<STR_LIT>")<EOL><DEDENT>_imgpointer = None<EOL>if keep_proportion is True:  <EOL><INDENT>if pixmap:  <EOL><INDENT>w = pixmap.width<EOL>h = pixmap.height<EOL><DEDENT>elif stream:  <EOL><INDENT>img_size = TOOLS.image_size(stream, keep_image=True)<EOL>w, h = img_size[:<NUM_LIT:2>]<EOL>stream = None  <EOL>_imgpointer = img_size[-<NUM_LIT:1>]  <EOL><DEDENT>else:  <EOL><INDENT>img = open(filename, "<STR_LIT:rb>")<EOL>stream = img.read()<EOL>img_size = TOOLS.image_size(stream, keep_image=True)<EOL>w, h = img_size[:<NUM_LIT:2>]<EOL>_imgpointer = img_size[-<NUM_LIT:1>]  <EOL>stream = None  <EOL>filename = None  <EOL>img.close()  <EOL><DEDENT>maxf = max(w, h).__float__()<EOL>fw = w / maxf<EOL>fh = h / maxf<EOL><DEDENT>else:<EOL><INDENT>fw = fh = <NUM_LIT:1.0><EOL><DEDENT>clip = r * ~page._getTransformation()  <EOL>matrix = calc_matrix(fw, fh, clip, rotate=rotate)<EOL>ilst = [i[<NUM_LIT:7>] for i in doc.getPageImageList(page.number)]<EOL>n = "<STR_LIT>"<EOL>i = <NUM_LIT:0><EOL>_imgname = n + "<STR_LIT:0>"<EOL>while _imgname in ilst:<EOL><INDENT>i += <NUM_LIT:1><EOL>_imgname = n + str(i)<EOL><DEDENT>page._insertImage(<EOL>filename=filename,  <EOL>pixmap=pixmap,  <EOL>stream=stream,  <EOL>matrix=matrix,  <EOL>overlay=overlay,<EOL>_imgname=_imgname,  <EOL>_imgpointer=_imgpointer,  <EOL>)<EOL>
Insert an image in a rectangle on the current page.

    Notes:
        Exactly one of filename, pixmap or stream must be provided.
    Args:
        rect: (rect-like) where to place the source image
        filename: (str) name of an image file
        pixmap: (obj) a Pixmap object
        stream: (bytes) an image in memory
        rotate: (int) degrees (multiple of 90)
        keep_proportion: (bool) whether to maintain aspect ratio
        overlay: (bool) put in foreground