Skip to content

Fix alpha of APNG frames blended with OP_OVER - #10582

Merged
radarhere merged 2 commits into
python-pillow:mainfrom
sohailshk:fix/apng-over-alpha
Nov 22, 2026
Merged

radarhere merged 2 commits into
python-pillow:mainfrom
sohailshk:fix/apng-over-alpha

Conversation

@sohailshk

@sohailshk sohailshk commented Nov 21, 2026 •

Copy link
Copy Markdown
Contributor

Changes proposed in this pull request:

  • RGBA and LA APNG frames using Blend.OP_OVER were pasted with the frame's alpha as a mask, which also interpolated the alpha channel. Over an opaque area, alpha 128 read back as 191 (spec: 255). Over a transparent area, (0, 0, 255, 128) read back as (0, 0, 128, 64) (spec: (0, 0, 255, 128)). Now they are alpha composited with Image.core.alpha_composite. Other modes are unchanged.
  • Added test_apng_blend_over_alpha for LA and RGBA.
  • Three existing assertions change: blend_op_over_near_transparent.png 97 -> 255, mode_16bit.png 191 -> 255 and mode_grayscale_alpha.png 191 -> 255. The test suite these fixtures come from (philip.html5.org, cited in Add APNG support #4243) states they "should be solid green" / "should be solid grey".
  • Tests: test_file_apng.py fails on main (4 failed, 54 passed) and passes with the change (58 passed); test_file_png.py, test_imagesequence.py and test_file_webp_animated.py pass (150 passed, 1 skipped).

The implementation were written with AI assistance.

Frames using APNG_BLEND_OP_OVER were applied with Image.paste() and the
frame's own alpha as mask, which also interpolates the alpha channel.
Drawing a partially transparent pixel over an opaque canvas therefore
made the canvas partially transparent (alpha 128 over alpha 255 gave
191). Use alpha compositing for RGBA and LA images instead.
@radarhere

Copy link
Copy Markdown
Member

For reference, https://wiki.mozilla.org/APNG_Specification

If blend_op is APNG_BLEND_OP_OVER the frame should be composited onto the output buffer based on its alpha

@radarhere
radarhere enabled auto-merge (squash) November 22, 2026 12:19
@radarhere
radarhere merged commit 688d01d into python-pillow:main Nov 22, 2026
55 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants