MCPcopy
hub / github.com/matplotlib/matplotlib / make_image

Method make_image

lib/matplotlib/image.py:1059–1134  ·  view source on GitHub ↗
(self, renderer, magnification=1.0, unsampled=False)

Source from the content-addressed store, hash-verified

1057 return False
1058
1059 def make_image(self, renderer, magnification=1.0, unsampled=False):
1060 # docstring inherited
1061 if self._A is None:
1062 raise RuntimeError('You must first set the image array')
1063 if unsampled:
1064 raise ValueError('unsampled not supported on NonUniformImage')
1065 A = self._A
1066 if A.ndim == 2:
1067 if A.dtype != np.uint8:
1068 A = self.to_rgba(A, bytes=True)
1069 else:
1070 A = np.repeat(A[:, :, np.newaxis], 4, 2)
1071 A[:, :, 3] = 255
1072 else:
1073 if A.dtype != np.uint8:
1074 A = (255*A).astype(np.uint8)
1075 if A.shape[2] == 3:
1076 B = np.zeros(tuple([*A.shape[0:2], 4]), np.uint8)
1077 B[:, :, 0:3] = A
1078 B[:, :, 3] = 255
1079 A = B
1080 magnified_extents = (self.axes.bbox.extents * magnification + 0.5).astype(int)
1081 l, b, r, t = magnified_extents / magnification
1082 width = int((r - l) * magnification)
1083 height = int((t - b) * magnification)
1084
1085 invertedTransform = self.axes.transData.inverted()
1086 x_pix_edges = invertedTransform.transform(
1087 [(x, b) for x in np.linspace(l, r, width + 1)])[:, 0]
1088 y_pix_edges = invertedTransform.transform(
1089 [(l, y) for y in np.linspace(b, t, height + 1)])[:, 1]
1090 x_pix_centers = (x_pix_edges[:-1] + x_pix_edges[1:]) / 2
1091 y_pix_centers = (y_pix_edges[:-1] + y_pix_edges[1:]) / 2
1092
1093 if self._interpolation == "nearest":
1094 x_mid = (self._Ax[:-1] + self._Ax[1:]) / 2
1095 y_mid = (self._Ay[:-1] + self._Ay[1:]) / 2
1096 x_int = x_mid.searchsorted(x_pix_centers)
1097 y_int = y_mid.searchsorted(y_pix_centers)
1098 # The following is equal to `A[y_int[:, None], x_int[None, :]]`,
1099 # but many times faster. Both casting to uint32 (to have an
1100 # effectively 1D array) and manual index flattening matter.
1101 im = (
1102 np.ascontiguousarray(A).view(np.uint32).ravel()[
1103 np.add.outer(y_int * A.shape[1], x_int)]
1104 .view(np.uint8).reshape((height, width, 4)))
1105 else: # self._interpolation == "bilinear"
1106 # Use np.interp to compute x_int/x_float has similar speed.
1107 x_int = np.clip(
1108 self._Ax.searchsorted(x_pix_centers) - 1, 0, len(self._Ax) - 2)
1109 y_int = np.clip(
1110 self._Ay.searchsorted(y_pix_centers) - 1, 0, len(self._Ay) - 2)
1111 idx_int = np.add.outer(y_int * A.shape[1], x_int)
1112 x_frac = np.clip(
1113 np.divide(x_pix_centers - self._Ax[x_int], np.diff(self._Ax)[x_int],
1114 dtype=np.float32), # Downcasting helps with speed.
1115 0, 1)
1116 y_frac = np.clip(

Callers

nothing calls this directly

Calls 5

IdentityTransformClass · 0.90
to_rgbaMethod · 0.45
invertedMethod · 0.45
transformMethod · 0.45
clipMethod · 0.45

Tested by

no test coverage detected