Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 24 additions & 13 deletions lib/matplotlib/image.py
Original file line number Diff line number Diff line change
Expand Up @@ -356,21 +356,29 @@ def _make_image(self, A, in_bbox, out_bbox, clip_bbox, magnification=1.0,
if not unsampled:
created_rgba_mask = False

if A.ndim not in (2, 3):
raise ValueError("Invalid dimensions, got %s" % (A.shape,))

if A.ndim == 2:
A = self.norm(A)
# If the image is greyscale, convert to RGBA with the
# correct alpha channel for resizing
rgba = np.empty((A.shape[0], A.shape[1], 4), dtype=A.dtype)
rgba[..., 0:3] = np.expand_dims(A, 2)
if A.dtype.kind == 'f':
# If the image is greyscale, convert to RGBA with the
# correct alpha channel for resizing
rgba = np.empty((A.shape[0], A.shape[1], 4), dtype=A.dtype)
rgba[..., 0] = A # normalized data
rgba[..., 1] = A < 0 # under data
# TODO: get threshold from the norm or colormap
rgba[..., 2] = A > 1 # over data
rgba[..., 3] = ~A.mask
A = rgba
output = np.zeros((out_height, out_width, 4),
dtype=A.dtype)
alpha = 1.0
created_rgba_mask = True
else:
rgba[..., 3] = np.where(A.mask, 0, np.iinfo(A.dtype).max)
A = rgba
output = np.zeros((out_height, out_width, 4), dtype=A.dtype)
alpha = 1.0
created_rgba_mask = True
elif A.ndim == 3:
A = self.cmap(A, alpha=self.get_alpha(), bytes=True)

if not created_rgba_mask:
# Always convert to RGBA, even if only RGB input
if A.shape[2] == 3:
A = _rgb_to_rgba(A)
Expand All @@ -382,8 +390,6 @@ def _make_image(self, A, in_bbox, out_bbox, clip_bbox, magnification=1.0,
alpha = self.get_alpha()
if alpha is None:
alpha = 1.0
else:
raise ValueError("Invalid dimensions, got %s" % (A.shape,))

_image.resample(
A, output, t, _interpd_[self.get_interpolation()],
Expand All @@ -393,8 +399,13 @@ def _make_image(self, A, in_bbox, out_bbox, clip_bbox, magnification=1.0,
if created_rgba_mask:
# Convert back to a masked greyscale array so
# colormapping works correctly
hid_output = output
output = np.ma.masked_array(
output[..., 0], output[..., 3] < 0.5)
hid_output[..., 0], hid_output[..., 3] < 0.5)
# relabel under data
output[hid_output[..., 1] > .5] = -1
# relabel over data
output[hid_output[..., 2] > .5] = 2

output = self.to_rgba(output, bytes=True, norm=False)

Expand Down