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

Function pil_to_array

lib/matplotlib/image.py:1725–1757  ·  view source on GitHub ↗

Load a `PIL image`_ and return it as a numpy int array. .. _PIL image: https://pillow.readthedocs.io/en/latest/reference/Image.html Returns ------- numpy.array The array shape depends on the image type: - (M, N) for grayscale images. - (M, N, 3) for R

(pilImage)

Source from the content-addressed store, hash-verified

1723
1724
1725def pil_to_array(pilImage):
1726 """
1727 Load a `PIL image`_ and return it as a numpy int array.
1728
1729 .. _PIL image: https://pillow.readthedocs.io/en/latest/reference/Image.html
1730
1731 Returns
1732 -------
1733 numpy.array
1734
1735 The array shape depends on the image type:
1736
1737 - (M, N) for grayscale images.
1738 - (M, N, 3) for RGB images.
1739 - (M, N, 4) for RGBA images.
1740 """
1741 if pilImage.mode in ['RGBA', 'RGBX', 'RGB', 'L']:
1742 # return MxNx4 RGBA, MxNx3 RBA, or MxN luminance array
1743 return np.asarray(pilImage)
1744 elif pilImage.mode.startswith('I;16'):
1745 # return MxN luminance array of uint16
1746 raw = pilImage.tobytes('raw', pilImage.mode)
1747 if pilImage.mode.endswith('B'):
1748 x = np.frombuffer(raw, '>u2')
1749 else:
1750 x = np.frombuffer(raw, '<u2')
1751 return x.reshape(pilImage.size[::-1]).astype('=u2')
1752 else: # try to convert to an rgba image
1753 try:
1754 pilImage = pilImage.convert('RGBA')
1755 except ValueError as err:
1756 raise RuntimeError('Unknown image mode') from err
1757 return np.asarray(pilImage) # return MxNx4 RGBA array
1758
1759
1760def _pil_png_to_float_array(pil_png):

Callers 2

set_dataMethod · 0.85
imreadFunction · 0.85

Calls 1

convertMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…