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

Method _pass_image_data

lib/matplotlib/colorizer.py:150–190  ·  view source on GitHub ↗

Helper function to pass ndarray of shape (...,3) or (..., 4) through `to_rgba()`, see `to_rgba()` for docstring.

(x, alpha=None, bytes=False, norm=True)

Source from the content-addressed store, hash-verified

148
149 @staticmethod
150 def _pass_image_data(x, alpha=None, bytes=False, norm=True):
151 """
152 Helper function to pass ndarray of shape (...,3) or (..., 4)
153 through `to_rgba()`, see `to_rgba()` for docstring.
154 """
155 if x.shape[2] == 3:
156 if alpha is None:
157 alpha = 1
158 if x.dtype == np.uint8:
159 alpha = np.uint8(alpha * 255)
160 m, n = x.shape[:2]
161 xx = np.empty(shape=(m, n, 4), dtype=x.dtype)
162 xx[:, :, :3] = x
163 xx[:, :, 3] = alpha
164 elif x.shape[2] == 4:
165 xx = x
166 else:
167 raise ValueError("Third dimension must be 3 or 4")
168 if xx.dtype.kind == 'f':
169 # If any of R, G, B, or A is nan, set to 0
170 if np.any(nans := np.isnan(x)):
171 if x.shape[2] == 4:
172 xx = xx.copy()
173 xx[np.any(nans, axis=2), :] = 0
174
175 if norm and (xx.max() > 1 or xx.min() < 0):
176 raise ValueError("Floating point image RGB values "
177 "must be in the [0,1] range")
178 if bytes:
179 xx = (xx * 255).astype(np.uint8)
180 elif xx.dtype == np.uint8:
181 if not bytes:
182 xx = xx.astype(np.float32) / 255
183 else:
184 raise ValueError("Image RGB array must be uint8 or "
185 "floating point; found %s" % xx.dtype)
186 # Account for any masked entries in the original array
187 # If any of R, G, B, or A are masked for an entry, we set alpha to 0
188 if np.ma.is_masked(x):
189 xx[np.any(np.ma.getmaskarray(x), axis=2), 3] = 0
190 return xx
191
192 def autoscale(self, A):
193 """

Callers 1

to_rgbaMethod · 0.95

Calls 3

maxMethod · 0.80
minMethod · 0.80
copyMethod · 0.45

Tested by

no test coverage detected