diff --git a/lib/matplotlib/image.py b/lib/matplotlib/image.py index f4045de4fa2c..ad20f862173a 100644 --- a/lib/matplotlib/image.py +++ b/lib/matplotlib/image.py @@ -390,8 +390,6 @@ def _make_image(self, A, in_bbox, out_bbox, clip_bbox, magnification=1.0, mask = A.mask # ~A.mask # masked data A = rgba - output = np.zeros((out_height, out_width, 4), - dtype=A.dtype) alpha = 1.0 created_rgba_mask = True else: @@ -414,15 +412,13 @@ def _make_image(self, A, in_bbox, out_bbox, clip_bbox, magnification=1.0, elif A.shape[2] != 4: raise ValueError("Invalid dimensions, got %s" % (A.shape,)) - output = np.zeros((out_height, out_width, 4), dtype=A.dtype) - alpha = self.get_alpha() - if alpha is None: - alpha = 1.0 + + output = np.zeros((out_height, out_width, 4), dtype=A.dtype) _image.resample( A, output, t, _interpd_[self.get_interpolation()], - self.get_resample(), alpha, + self.get_resample(), alpha if alpha is not None else 1, self.get_filternorm() or 0.0, self.get_filterrad() or 0.0) if created_rgba_mask: @@ -447,7 +443,7 @@ def _make_image(self, A, in_bbox, out_bbox, clip_bbox, magnification=1.0, # and the pixels that Agg is telling us to ignore (relavent # to non-affine transforms) # Use half alpha as the threshold for pixels to mask. - out_mask = out_mask | (hid_output[..., 3] < .5) + out_mask = out_mask | (hid_output[..., 3] == 0) output = np.ma.masked_array( hid_output[..., 0], out_mask) @@ -465,9 +461,9 @@ def _make_image(self, A, in_bbox, out_bbox, clip_bbox, magnification=1.0, # Apply alpha *after* if the input was greyscale without a mask if A.ndim == 2 or created_rgba_mask: + alpha_channel = output[:, :, 3] alpha = self.get_alpha() if alpha is not None and alpha != 1.0: - alpha_channel = output[:, :, 3] alpha_channel[:] = np.asarray( np.asarray(alpha_channel, np.float32) * alpha, np.uint8) diff --git a/lib/matplotlib/tests/baseline_images/test_image/rotate_image.png b/lib/matplotlib/tests/baseline_images/test_image/rotate_image.png index 322e8c00478f..3955bf39a85d 100644 Binary files a/lib/matplotlib/tests/baseline_images/test_image/rotate_image.png and b/lib/matplotlib/tests/baseline_images/test_image/rotate_image.png differ diff --git a/lib/matplotlib/tests/test_image.py b/lib/matplotlib/tests/test_image.py index d0e9c97c9747..1f2094c2b153 100644 --- a/lib/matplotlib/tests/test_image.py +++ b/lib/matplotlib/tests/test_image.py @@ -24,7 +24,7 @@ from numpy.testing import ( assert_array_equal, assert_array_almost_equal, assert_allclose) from matplotlib.testing.noseclasses import KnownFailureTest -from copy import copy +from copy import deepcopy from numpy import ma import matplotlib.image as mimage import matplotlib.colors as colors @@ -155,6 +155,7 @@ def test_imsave(): assert_array_equal(arr_dpi1, arr_dpi100) +@cleanup def test_imsave_color_alpha(): # Test that imsave accept arrays with ndim=3 where the third dimension is # color and alpha without raising any exceptions, and that the data is @@ -698,7 +699,7 @@ def test_mask_image_over_under(): Z2 = mlab.bivariate_normal(X, Y, 1.5, 0.5, 1, 1) Z = 10*(Z2 - Z1) # difference of Gaussians - palette = copy(plt.cm.gray) + palette = deepcopy(plt.cm.gray) palette.set_over('r', 1.0) palette.set_under('g', 1.0) palette.set_bad('b', 1.0) @@ -761,7 +762,7 @@ def test_imshow_endianess(): remove_text=True, style='default') def test_imshow_masked_interpolation(): - cm = copy(plt.get_cmap('viridis')) + cm = deepcopy(plt.get_cmap('viridis')) cm.set_over('r') cm.set_under('b') cm.set_bad('k')