8000 Backport PR #12062: Separate alpha and rbg interpolation then recombi… · matplotlib/matplotlib@da1245f · GitHub
[go: up one dir, main page]

Skip to content

Commit da1245f

Browse files
dstansbyMeeseeksDev[bot]
authored andcommitted
Backport PR #12062: Separate alpha and rbg interpolation then recombine to fix issue11316
1 parent 334d875 commit da1245f

File tree

7 files changed

+65
-41
lines changed

7 files changed

+65
-41
lines changed

lib/matplotlib/image.py

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -489,16 +489,31 @@ def _make_image(self, A, in_bbox, out_bbox, clip_bbox, magnification=1.0,
489489
.format(A.shape))
490490

491491
output = np.zeros((out_height, out_width, 4), dtype=A.dtype)
492+
output_a = np.zeros((out_height, out_width), dtype=A.dtype)
492493

493494
alpha = self.get_alpha()
494495
if alpha is None:
495-
alpha = 1.0
496+
alpha = 1
497+
498+
#resample alpha channel
499+
alpha_channel = A[..., 3]
500+
_image.resample(
501+
alpha_channel, output_a, t,
502+
_interpd_[self.get_interpolation()],
503+
self.get_resample(), alpha,
504+
self.get_filternorm(), self.get_filterrad())
496505

506+
#resample rgb channels
507+
A = _rgb_to_rgba(A[..., :3])
497508
_image.resample(
498-
A, output, t, _interpd_[self.get_interpolation()],
509+
A, output, t,
510+
_interpd_[self.get_interpolation()],
499511
self.get_resample(), alpha,
500512
self.get_filternorm(), self.get_filterrad())
501513

514+
#recombine rgb and alpha channels
515+
output[..., 3] = output_a
516+
502517
# at this point output is either a 2D array of normed data
503518
# (of int or float)
504519
# or an RGBA array of re-sampled input
Binary file not shown.
Loading

lib/matplotlib/tests/baseline_images/test_image/image_composite_alpha.svg

Lines changed: 34 additions & 39 deletions
Loading
Loading
Loading

lib/matplotlib/tests/test_image.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,20 @@ def test_image_interps():
4545
ax3.set_ylabel('bicubic')
4646

4747

48+
@image_comparison(baseline_images=['interp_alpha'],
49+
extensions=['png'], remove_text=True)
50+
def test_alpha_interp():
51+
'Test the interpolation of the alpha channel on RGBA images'
52+
fig, (axl, axr) = plt.subplots(1, 2)
53+
# full green image
54+
img = np.zeros((5, 5, 4))
55+
img[..., 1] = np.ones((5, 5))
56+
# transparent under main diagonal
57+
img[..., 3] = np.tril(np.ones((5, 5), dtype=np.uint8))
58+
axl.imshow(img, interpolation="none")
59+
axr.imshow(img, interpolation="bilinear")
60+
61+
4862
@image_comparison(baseline_images=['interp_nearest_vs_none'],
4963
extensions=['pdf', 'svg'], remove_text=True)
5064
def test_interp_nearest_vs_none():

0 commit comments

Comments
 (0)
0