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

Skip to content

Commit 625e80a

Browse files
committed
Fix alpha handling for interpolation == 'none'
1 parent 5073f6e commit 625e80a

File tree

6 files changed

+443
-4
lines changed

6 files changed

+443
-4
lines changed

lib/matplotlib/backends/backend_pdf.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1602,6 +1602,10 @@ def draw_image(self, gc, x, y, im, transform=None):
16021602
if w == 0 or h == 0:
16031603
return
16041604

1605+
if transform is None:
1606+
# If there's no transform, alpha has already been applied
1607+
gc.set_alpha(1.0)
1608+
16051609
self.check_gc(gc)
16061610

16071611
w = 72.0 * w / self.image_dpi

lib/matplotlib/image.py

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -294,6 +294,8 @@ def _make_image(self, A, in_bbox, out_bbox, clip_bbox, magnification=1.0,
294294
output[...] = -100.0
295295
else:
296296
output = np.zeros((out_height, out_width), dtype=A.dtype)
297+
298+
alpha = 1.0
297299
elif len(A.shape) == 3:
298300
# Always convert to RGBA, even if only RGB input
299301
if A.shape[2] == 3:
@@ -302,19 +304,27 @@ def _make_image(self, A, in_bbox, out_bbox, clip_bbox, magnification=1.0,
302304
raise ValueError("Invalid dimensions, got %s" % (A.shape,))
303305

304306
output = np.zeros((out_height, out_width, 4), dtype=A.dtype)
307+
308+
alpha = self.get_alpha()
309+
if alpha is None:
310+
alpha = 1.0
305311
else:
306312
raise ValueError("Invalid dimensions, got %s" % (A.shape,))
307313

308-
alpha = self.get_alpha()
309-
if alpha is None:
310-
alpha = 1.0
311-
312314
_image.resample(
313315
A, output, t, _interpd_[self.get_interpolation()],
314316
self.get_resample(), alpha,
315317
self.get_filternorm() or 0.0, self.get_filterrad() or 0.0)
316318

317319
output = self.to_rgba(output, bytes=True, norm=False)
320+
321+
# Apply alpha *after* if the input was greyscale
322+
if len(A.shape) == 2:
323+
alpha = self.get_alpha()
324+
if alpha is not None and alpha != 1.0:
325+
alpha_channel = output[:, :, 3]
326+
alpha_channel[:] = np.asarray(
327+
np.asarray(alpha_channel, np.float) * alpha, np.uint8)
318328
else:
319329
if self._imcache is None:
320330
self._imcache = self.to_rgba(A, bytes=True, norm=(A.ndim == 2))
Binary file not shown.

0 commit comments

Comments
 (0)