Skip to content

Fix alpha handling when flattening images for vector backends - #32107

Merged
QuLogic merged 1 commit into
matplotlib:mainfrom
ayshih:flatten_images_with_alpha
Aug 6, 2026
Merged

Fix alpha handling when flattening images for vector backends#32107
QuLogic merged 1 commit into
matplotlib:mainfrom
ayshih:flatten_images_with_alpha

Conversation

@ayshih

@ayshih ayshih commented Jul 23, 2026

Copy link
Copy Markdown
Contributor

PR summary

This PR fixes a bug when flattening images for vector backends and a non-unity scalar alpha is specified. Image flattening is when the Agg resampler is used to flatten a sequence of images into a single image so that only a single image needs to be drawn by the backend, and is enabled by default. When an image is added with a scalar alpha (i.e., imshow(..., alpha=...)), that scalar alpha is mistakenly applied twice during flattening: (1) when preparing the image to be resampled, and (2) when resampling the prepared image into the output buffer. For example, an image that is intended to have an alpha of 0.6 ends up in the flattened image as if it had an alpha of 0.36 (0.6 squared). This is the root cause of #8847.

I also added a clarifying comment for future maintainers because I got tripped up with inspecting the code. The Agg resampler typically assumes that the input image is in premultiplied alpha space (cf. #31021 (comment)), except when the input image is uint8 (code). I forgot about that exception.

Fixes #8847

AI Disclosure

PR checklist

@ayshih
ayshih force-pushed the flatten_images_with_alpha branch 5 times, most recently from ca23deb to 86aeef6 Compare July 24, 2026 02:52
@ayshih
ayshih force-pushed the flatten_images_with_alpha branch from 86aeef6 to 98a50a6 Compare July 24, 2026 03:16
@ayshih
ayshih marked this pull request as ready for review July 24, 2026 03:52
@QuLogic
QuLogic merged commit ac55a36 into matplotlib:main Aug 6, 2026
41 checks passed
@QuLogic QuLogic added this to the v3.12.0 milestone Aug 6, 2026
@ayshih
ayshih deleted the flatten_images_with_alpha branch August 6, 2026 20:34
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.

Agg alpha image compositor differs from Matplotlib's software image compositor

3 participants