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

Skip to content

Fix TkCairo bug when displaying images with partially transparent pixels - #32315

Merged
QuLogic merged 1 commit into
matplotlib:mainfrom
ayshih:tkcairo_fix
Sep 8, 2026
Merged

Fix TkCairo bug when displaying images with partially transparent pixels#32315
QuLogic merged 1 commit into
matplotlib:mainfrom
ayshih:tkcairo_fix

Conversation

@ayshih

@ayshih ayshih commented Sep 8, 2026

Copy link
Copy Markdown
Contributor

PR summary

For the TkCairo GUI backend, the Cairo image buffer uses premultiplied alpha, and is currently passed directly to Tk for blitting, but Tk (PhotoImage) expects unmultiplied alpha. Thus, partially transparent pixels – which usually requires making the Figure and Axes patches transparent – are not displayed correctly. This bug is only in the GUI; saving the image produces the correct output.

This PR uses an existing cbook function to convert from premultiplied-space alpha to unmultiplied-alpha space, and also adds a fast bailout path to that function when the image is fully opaque.

Example

import matplotlib
matplotlib.use('TkCairo')

import matplotlib.pyplot as plt
from matplotlib.patches import Rectangle

fig, ax = plt.subplots()
fig.set_facecolor('none')
ax.set_facecolor('none')
ax.set_axis_off()

# Construct a patch that is 50% transparent
rect1 = Rectangle((0, 0), 1, 0.5, facecolor=(1, 0, 0, 0.5))
ax.add_patch(rect1)
ax.text(0.5, 0.25, 'Test', ha='center', va='center')

# Construct an opaque patch with the expected color when the above is composited with a white backdrop
rect2 = Rectangle((0, 0.5), 1, 0.5, facecolor=(1, 0.5, 0.5, 1))
ax.add_patch(rect2)
ax.text(0.5, 0.75, 'Reference', ha='center', va='center')

plt.show()

Before this PR

tkcairo before

After this PR

tkcairo after

AI Disclosure

No AI was used

PR quality check

  • Use an expressive title, e.g. "Fix title font property precedence"
  • [N/A] New and changed code is tested
  • [N/A] Plotting related features are demonstrated in an example
  • [N/A] New features and API changes have release notes
  • [N/A] Documentation complies with general and docstring guidelines

@ayshih ayshih changed the title Fix TkCairo bug when displaying images with fractional alpha Fix TkCairo bug when displaying images with partially transparent pixels Sep 8, 2026
@ayshih
ayshih marked this pull request as ready for review September 8, 2026 13:54
@ayshih

ayshih commented Sep 8, 2026

Copy link
Copy Markdown
Contributor Author

I'll note that wxCairo also does not correctly display partially transparent pixels, but the situation there requires further investigation because wxAgg is also wrong.

(2, 1, 0, 3) if sys.byteorder == "little" else (1, 2, 3, 0))
premult_argb = np.reshape(surface.get_data(), (height, width, 4))
unmult_rgba = cbook._premultiplied_argb32_to_unmultiplied_rgba8888(premult_argb)
_backend_tk.blit(self._tkphoto, unmult_rgba, (0, 1, 2, 3))

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The removal of byteorder confused me until I realized that _premultiplied_argb32_to_unmultiplied_rgba8888 was handling it.

I don't think we need a comment in the code about this, as it only applies to the diff.

@iccir iccir left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me!

I'm wondering if there are performance wins if we moved the premultiply conversion functions into C++, but we can investigate that in the future if it shows up in a profile.

@iccir

iccir commented Sep 8, 2026

Copy link
Copy Markdown
Contributor

I'll note that wxCairo also does not correctly display partially transparent pixels, but the situation there requires further investigation because wxAgg is also wrong.

On my machine, the wx issues are related to color space conversion between sRGB and the device profile. I can get the correct colors by telling macOS that the device profile is sRGB.

@QuLogic QuLogic added this to the v3.11.2 milestone Sep 8, 2026
@QuLogic
QuLogic merged commit 284213d into matplotlib:main Sep 8, 2026
40 of 41 checks passed
QuLogic added a commit that referenced this pull request Sep 8, 2026
QuLogic added a commit that referenced this pull request Sep 8, 2026
…315-on-v3.11.x

Backport PR #32315 on branch v3.11.x (Fix TkCairo bug when displaying images with partially transparent pixels)
@ayshih
ayshih deleted the tkcairo_fix branch September 8, 2026 20:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants