MCPcopy Index your code
hub / github.com/matplotlib/matplotlib / draw_image

Method draw_image

lib/matplotlib/backends/backend_svg.py:953–1022  ·  view source on GitHub ↗
(self, gc, x, y, im, transform=None)

Source from the content-addressed store, hash-verified

951 return self.image_dpi / 72.0
952
953 def draw_image(self, gc, x, y, im, transform=None):
954 # docstring inherited
955
956 h, w = im.shape[:2]
957
958 if w == 0 or h == 0:
959 return
960
961 clip_attrs = self._get_clip_attrs(gc)
962 if clip_attrs:
963 # Can't apply clip-path directly to the image because the image has
964 # a transformation, which would also be applied to the clip-path.
965 self.writer.start('g', **clip_attrs)
966
967 url = gc.get_url()
968 if url is not None:
969 self.writer.start('a', attrib={'xlink:href': url, 'target': '_blank'})
970
971 attrib = {}
972 oid = gc.get_gid()
973 if mpl.rcParams['svg.image_inline']:
974 buf = BytesIO()
975 Image.fromarray(im).save(buf, format="png")
976 oid = oid or self._make_id('image', buf.getvalue())
977 attrib['xlink:href'] = (
978 "data:image/png;base64,\n" +
979 base64.b64encode(buf.getvalue()).decode('ascii'))
980 else:
981 if self.basename is None:
982 raise ValueError("Cannot save image data to filesystem when "
983 "writing SVG to an in-memory buffer")
984 filename = f'{self.basename}.image{next(self._image_counter)}.png'
985 _log.info('Writing image file for inclusion: %s', filename)
986 Image.fromarray(im).save(filename)
987 oid = oid or 'Im_' + self._make_id('image', filename)
988 attrib['xlink:href'] = filename
989 attrib['id'] = oid
990
991 if transform is None:
992 w = 72.0 * w / self.image_dpi
993 h = 72.0 * h / self.image_dpi
994 self.writer.element(
995 'image',
996 transform=_generate_transform([
997 ('scale', (1, -1)), ('translate', (0, -h))]),
998 x=_short_float_fmt(x),
999 y=_short_float_fmt(-(self.height - y - h)),
1000 width=_short_float_fmt(w), height=_short_float_fmt(h),
1001 attrib=attrib,
1002 )
1003 else:
1004 alpha = gc.get_alpha()
1005 if alpha != 1.0:
1006 attrib['opacity'] = _short_float_fmt(alpha)
1007 flipped = (
1008 Affine2D().scale(1 / w, 1 / h)
1009 + transform
1010 + Affine2D().translate(x, y).scale(1, -1).translate(0, self.height))

Callers

nothing calls this directly

Calls 15

_get_clip_attrsMethod · 0.95
_make_idMethod · 0.95
Affine2DClass · 0.90
_generate_transformFunction · 0.85
_short_float_fmtFunction · 0.85
saveMethod · 0.80
elementMethod · 0.80
translateMethod · 0.80
startMethod · 0.45
get_urlMethod · 0.45
get_gidMethod · 0.45
get_alphaMethod · 0.45

Tested by

no test coverage detected