Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit eda0912

Browse files
committed
Fix #6069. Handle image masks correctly
Alternative to #6070 This works by converting the greyscale image to an RGBA image (with the alpha channel created correctly) prior to resizing in Agg. This also removes the hack (that caused problems with over/under) where we were using negative values to indicate transparency.
1 parent 4fc85b0 commit eda0912

File tree

7 files changed

+403
-15
lines changed

7 files changed

+403
-15
lines changed

lib/matplotlib/cm.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -269,10 +269,6 @@ def to_rgba(self, x, alpha=None, bytes=False, norm=True):
269269
if norm:
270270
x = self.norm(x)
271271
rgba = self.cmap(x, alpha=alpha, bytes=bytes)
272-
# For floating-point greyscale images, we treat negative as
273-
# transparent so we copy that over to the alpha channel
274-
if x.ndim == 2 and x.dtype.kind == 'f':
275-
rgba[:, :, 3][x < 0.0] = 0
276272
return rgba
277273

278274
def set_array(self, A):

lib/matplotlib/image.py

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -354,20 +354,22 @@ def _make_image(self, A, in_bbox, out_bbox, clip_bbox, magnification=1.0,
354354
out_height = int(out_height_base)
355355

356356
if not unsampled:
357+
created_rgba_mask = False
358+
357359
if A.ndim == 2:
358360
A = self.norm(A)
361+
# If the image is greyscale, convert to RGBA with the
362+
# correct alpha channel for resizing
363+
rgba = np.empty((A.shape[0], A.shape[1], 4), dtype=A.dtype)
364+
rgba[..., 0:3] = np.expand_dims(A, 2)
359365
if A.dtype.kind == 'f':
360-
# For floating-point greyscale images, we treat negative
361-
# numbers as transparent.
362-
363-
# TODO: Use np.full when we support Numpy 1.9 as a
364-
# minimum
365-
output = np.empty((out_height, out_width), dtype=A.dtype)
366-
output[...] = -100.0
366+
rgba[..., 3] = ~A.mask
367367
else:
368-
output = np.zeros((out_height, out_width), dtype=A.dtype)
369-
368+
rgba[..., 3] = np.where(A.mask, 0, np.iinfo(np.dtype).max)
369+
A = rgba
370+
output = np.zeros((out_height, out_width, 4), dtype=A.dtype)
370371
alpha = 1.0
372+
created_rgba_mask = True
371373
elif A.ndim == 3:
372374
# Always convert to RGBA, even if only RGB input
373375
if A.shape[2] == 3:
@@ -388,10 +390,16 @@ def _make_image(self, A, in_bbox, out_bbox, clip_bbox, magnification=1.0,
388390
self.get_resample(), alpha,
389391
self.get_filternorm() or 0.0, self.get_filterrad() or 0.0)
390392

393+
if created_rgba_mask:
394+
# Convert back to a masked greyscale array so
395+
# colormapping works correctly
396+
output = np.ma.masked_array(
397+
output[..., 0], output[..., 3] < 0.5)
398+
391399
output = self.to_rgba(output, bytes=True, norm=False)
392400

393-
# Apply alpha *after* if the input was greyscale
394-
if A.ndim == 2:
401+
# Apply alpha *after* if the input was greyscale without a mask
402+
if A.ndim == 2 or created_rgba_mask:
395403
alpha = self.get_alpha()
396404
if alpha is not None and alpha != 1.0:
397405
alpha_channel = output[:, :, 3]
Binary file not shown.

0 commit comments

Comments
 (0)