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

Skip to content

Commit 284213d

Browse files
authored
Merge pull request #32315 from ayshih/tkcairo_fix
Fix TkCairo bug when displaying images with partially transparent pixels
2 parents 8a621d3 + 9467504 commit 284213d

2 files changed

Lines changed: 6 additions & 6 deletions

File tree

lib/matplotlib/backends/backend_tkcairo.py

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
1-
import sys
2-
31
import numpy as np
42

3+
from .. import cbook
54
from . import _backend_tk
65
from .backend_cairo import cairo, FigureCanvasCairo
76
from ._backend_tk import _BackendTk, FigureCanvasTk
@@ -14,10 +13,9 @@ def draw(self):
1413
self._renderer.set_context(cairo.Context(surface))
1514
self._renderer.dpi = self.figure.dpi
1615
self.figure.draw(self._renderer)
17-
buf = np.reshape(surface.get_data(), (height, width, 4))
18-
_backend_tk.blit(
19-
self._tkphoto, buf,
20-
(2, 1, 0, 3) if sys.byteorder == "little" else (1, 2, 3, 0))
16+
premult_argb = np.reshape(surface.get_data(), (height, width, 4))
17+
unmult_rgba = cbook._premultiplied_argb32_to_unmultiplied_rgba8888(premult_argb)
18+
_backend_tk.blit(self._tkphoto, unmult_rgba, (0, 1, 2, 3))
2119

2220

2321
@_BackendTk.export

lib/matplotlib/cbook.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2210,6 +2210,8 @@ def _premultiplied_argb32_to_unmultiplied_rgba8888(buf):
22102210
[2, 1, 0, 3] if sys.byteorder == "little" else [1, 2, 3, 0], axis=2)
22112211
rgb = rgba[..., :-1]
22122212
alpha = rgba[..., -1]
2213+
if alpha.min() == 0xff:
2214+
return rgba
22132215
# Un-premultiply alpha. The formula is the same as in cairo-png.c.
22142216
mask = alpha != 0
22152217
for channel in np.rollaxis(rgb, -1):

0 commit comments

Comments
 (0)