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

Method _writeImg

lib/matplotlib/backends/backend_pdf.py:1651–1712  ·  view source on GitHub ↗

Write the image *data*, of shape ``(height, width, 1)`` (grayscale) or ``(height, width, 3)`` (RGB), as pdf object *id* and with the soft mask (alpha channel) *smask*, which should be either None or a ``(height, width, 1)`` array.

(self, data, id, smask=None)

Source from the content-addressed store, hash-verified

1649 return png_data, bit_depth, palette
1650
1651 def _writeImg(self, data, id, smask=None):
1652 """
1653 Write the image *data*, of shape ``(height, width, 1)`` (grayscale) or
1654 ``(height, width, 3)`` (RGB), as pdf object *id* and with the soft mask
1655 (alpha channel) *smask*, which should be either None or a ``(height,
1656 width, 1)`` array.
1657 """
1658 height, width, color_channels = data.shape
1659 obj = {'Type': Name('XObject'),
1660 'Subtype': Name('Image'),
1661 'Width': width,
1662 'Height': height,
1663 'ColorSpace': Name({1: 'DeviceGray', 3: 'DeviceRGB'}[color_channels]),
1664 'BitsPerComponent': 8}
1665 if smask:
1666 obj['SMask'] = smask
1667 if mpl.rcParams['pdf.compression']:
1668 if data.shape[-1] == 1:
1669 data = data.squeeze(axis=-1)
1670 png = {'Predictor': 10, 'Colors': color_channels, 'Columns': width}
1671 img = Image.fromarray(data)
1672 img_colors = img.getcolors(maxcolors=256)
1673 if color_channels == 3 and img_colors is not None:
1674 # Convert to indexed color if there are 256 colors or fewer. This can
1675 # significantly reduce the file size.
1676 num_colors = len(img_colors)
1677 palette = np.array([comp for _, color in img_colors for comp in color],
1678 dtype=np.uint8)
1679 palette24 = ((palette[0::3].astype(np.uint32) << 16) |
1680 (palette[1::3].astype(np.uint32) << 8) |
1681 palette[2::3])
1682 rgb24 = ((data[:, :, 0].astype(np.uint32) << 16) |
1683 (data[:, :, 1].astype(np.uint32) << 8) |
1684 data[:, :, 2])
1685 indices = np.argsort(palette24).astype(np.uint8)
1686 rgb8 = indices[np.searchsorted(palette24, rgb24, sorter=indices)]
1687 img = Image.fromarray(rgb8).convert("P")
1688 img.putpalette(palette)
1689 png_data, bit_depth, palette = self._writePng(img)
1690 if bit_depth is None or palette is None:
1691 raise RuntimeError("invalid PNG header")
1692 palette = palette[:num_colors * 3] # Trim padding; remove for Pillow>=9
1693 obj['ColorSpace'] = [Name('Indexed'), Name('DeviceRGB'),
1694 num_colors - 1, palette]
1695 obj['BitsPerComponent'] = bit_depth
1696 png['Colors'] = 1
1697 png['BitsPerComponent'] = bit_depth
1698 else:
1699 png_data, _, _ = self._writePng(img)
1700 else:
1701 png = None
1702 self.beginStream(
1703 id,
1704 self.reserveObject('length of image stream'),
1705 obj,
1706 png=png
1707 )
1708 if png:

Callers 1

writeImagesMethod · 0.95

Calls 7

_writePngMethod · 0.95
beginStreamMethod · 0.95
reserveObjectMethod · 0.95
endStreamMethod · 0.95
NameClass · 0.85
convertMethod · 0.45
writeMethod · 0.45

Tested by

no test coverage detected