Skip to content

Modified scatter method to correctly recognize RGB/A values #6087

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 16 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Merge pull request #6099 from mdboom/masked-image
Fix #6069.  Handle image masks correctly
  • Loading branch information
tacaswell committed Mar 4, 2016
commit 1302606cbfe03ca8d00eb80f99b33e1b11091b65
4 changes: 0 additions & 4 deletions lib/matplotlib/cm.py
Original file line number Diff line number Diff line change
Expand Up @@ -269,10 +269,6 @@ def to_rgba(self, x, alpha=None, bytes=False, norm=True):
if norm:
x = self.norm(x)
rgba = self.cmap(x, alpha=alpha, bytes=bytes)
# For floating-point greyscale images, we treat negative as
# transparent so we copy that over to the alpha channel
if x.ndim == 2 and x.dtype.kind == 'f':
rgba[:, :, 3][x < 0.0] = 0
return rgba

def set_array(self, A):
Expand Down
30 changes: 19 additions & 11 deletions lib/matplotlib/image.py
Original file line number Diff line number Diff line change
Expand Up @@ -354,20 +354,22 @@ def _make_image(self, A, in_bbox, out_bbox, clip_bbox, magnification=1.0,
out_height = int(out_height_base)

if not unsampled:
created_rgba_mask = False

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':
# For floating-point greyscale images, we treat negative
# numbers as transparent.

# TODO: Use np.full when we support Numpy 1.9 as a
# minimum
output = np.empty((out_height, out_width), dtype=A.dtype)
output[...] = -100.0
rgba[..., 3] = ~A.mask
else:
output = np.zeros((out_height, out_width), dtype=A.dtype)

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:
# Always convert to RGBA, even if only RGB input
if A.shape[2] == 3:
Expand All @@ -388,10 +390,16 @@ def _make_image(self, A, in_bbox, out_bbox, clip_bbox, magnification=1.0,
self.get_resample(), alpha,
self.get_filternorm() or 0.0, self.get_filterrad() or 0.0)

if created_rgba_mask:
# Convert back to a masked greyscale array so
# colormapping works correctly
output = np.ma.masked_array(
output[..., 0], output[..., 3] < 0.5)

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

# Apply alpha *after* if the input was greyscale
if A.ndim == 2:
# Apply alpha *after* if the input was greyscale without a mask
if A.ndim == 2 or created_rgba_mask:
alpha = self.get_alpha()
if alpha is not None and alpha != 1.0:
alpha_channel = output[:, :, 3]
Expand Down
Binary file not shown.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading