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

Skip to content

Commit f157cba

Browse files
committed
respect array alpha with interpolation_stage='rgba'
1 parent e353367 commit f157cba

File tree

2 files changed

+29
-10
lines changed

2 files changed

+29
-10
lines changed

lib/matplotlib/image.py

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -553,17 +553,25 @@ def _make_image(self, A, in_bbox, out_bbox, clip_bbox, magnification=1.0,
553553
if A.ndim == 2: # _interpolation_stage == 'rgba'
554554
self.norm.autoscale_None(A)
555555
A = self.to_rgba(A)
556-
alpha = self._get_scalar_alpha()
557-
if A.shape[2] == 3:
558-
# No need to resample alpha or make a full array; NumPy will expand
559-
# this out and cast to uint8 if necessary when it's assigned to the
560-
# alpha channel below.
561-
output_alpha = (255 * alpha) if A.dtype == np.uint8 else alpha
556+
557+
alpha = self.get_alpha()
558+
if alpha is not None and np.ndim(alpha) > 0:
559+
output_alpha = _resample(self, alpha, out_shape, t, resample=True)
560+
output = _resample( # resample rgb channels
561+
# alpha: float, should only be specified when alpha is a scalar
562+
self, _rgb_to_rgba(A[..., :3]), out_shape, t)
562563
else:
563-
output_alpha = _resample( # resample alpha channel
564-
self, A[..., 3], out_shape, t, alpha=alpha)
565-
output = _resample( # resample rgb channels
566-
self, _rgb_to_rgba(A[..., :3]), out_shape, t, alpha=alpha)
564+
alpha = self._get_scalar_alpha()
565+
if A.shape[2] == 3:
566+
# No need to resample alpha or make a full array; NumPy will
567+
# expand this out and cast to uint8 if necessary when it's
568+
# assigned to the alpha channel below.
569+
output_alpha = (255 * alpha) if A.dtype == np.uint8 else alpha
570+
else:
571+
output_alpha = _resample( # resample alpha channel
572+
self, A[..., 3], out_shape, t, alpha=alpha)
573+
output = _resample( # resample rgb channels
574+
self, _rgb_to_rgba(A[..., :3]), out_shape, t, alpha=alpha)
567575
output[..., 3] = output_alpha # recombine rgb and alpha
568576

569577
# output is now either a 2D array of normed (int or float) data

lib/matplotlib/tests/test_image.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1578,6 +1578,17 @@ def test_non_transdata_image_does_not_touch_aspect():
15781578
assert ax.get_aspect() == 2
15791579

15801580

1581+
@check_figures_equal()
1582+
def test_interpolation_stage_rgba_does_not_respect_array_alpha(fig_test, fig_ref):
1583+
# GH 28382
1584+
im = np.arange(9).reshape(3, 3)
1585+
alpha = np.linspace(0, 1, 9).reshape(3, 3)
1586+
ax_test = fig_test.subplots()
1587+
ax_test.imshow(im, alpha=alpha, interpolation='none', interpolation_stage='rgba')
1588+
ax_ref = fig_ref.subplots()
1589+
ax_ref.imshow(im, alpha=alpha, interpolation='none')
1590+
1591+
15811592
@pytest.mark.parametrize(
15821593
'dtype',
15831594
('float64', 'float32', 'int16', 'uint16', 'int8', 'uint8'),

0 commit comments

Comments
 (0)