Skip to content

Preserve color-key transparency when inverting images - #10722

Open
Ap-95671 wants to merge 1 commit into
python-pillow:mainfrom
Ap-95671:codex/pillow-preserve-invert-transparency
Open

Ap-95671 wants to merge 1 commit into
python-pillow:mainfrom
Ap-95671:codex/pillow-preserve-invert-transparency

Conversation

@Ap-95671

@Ap-95671 Ap-95671 commented Dec 3, 2026

Copy link
Copy Markdown

Problem

ImageOps.invert() inverts pixel samples but carries the old color key forward. A PNG with a transparent sample and its opaque complement can go from alpha bytes [0, 255] to [255, 0] after inversion.

Change

Apply the inversion lookup table to the transparency key for modes 1, L, and RGB. This preserves the mode and transparent pixels, including when saving the result to PNG. Palette inversion continues to preserve its existing alpha handling. The input image metadata is preserved.

Verification

  • Reproduced both bilevel keys, a grayscale key, and an RGB key before the fix. All four regression cases pass with the fix.
  • Tested 384 grayscale and RGB PNG round trips.
  • ImageOps/PNG tests: 159 passed. Pillow selftest: 59 passed.
  • Broad suite: 6,176 passed, 116 skipped, 3 xfailed, and one macOS screen-capture test deselected after it failed in the headless environment.
  • Ruff, Black, Sphinx lint, and git diff --check passed.

Prepared with OpenAI Codex.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant