Repository navigation
Preserve alpha channel when saving RGBA images as BMP - #10512
Open
anishpentyala wants to merge 3 commits into
Open
anishpentyala wants to merge 3 commits into
anishpentyala wants to merge 3 commits into
Conversation
Updated release notes for version 13.1.0 to include changes regarding BMP saving and zlib dependency.
Added tests for RGBA handling in BMP format.
Updated BMP header handling to support RGBA images with BITMAPV4HEADER and added channel masks.
Member
|
Hi. Judging from your profile, you have used AI to create this PR. If this feature would be helpful to you personally in some way, please let us know. However, if this was created purely because AI had an idea, I would rather prioritize other work that we know would help people. |
Author
|
Hi, I convert images to BMP for a custom ESP32 display interface to avoid JPEG or PNG decoding, and my reader supports V4 headers. Preserving alpha would be useful in that workflow when working with transparent images. Thank you for taking a look! |
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
When saving an RGBA image as BMP, Pillow writes a 40-byte BITMAPINFOHEADER with BI_RGB compression. The alpha bytes are present in the pixel data, but the header gives readers no way to know the 4th byte is alpha rather than padding - Pillow's own reader therefore reloads such files in mode RGB (BGRX), and the alpha channel is unrecoverable from a Pillow round trip. (Per the BMP spec, the 4th component in a BI_RGB RGBQUAD is reserved, not
alpha; Pillow's reader deliberately does not assume alpha there - see the comment referencing #1293.)
Pillow's BMP reader already honors an explicit alpha channel when a file declares one via a BITMAPV4/V5HEADER or BI_BITFIELDS masks (see also the read-side USE_RAW_ALPHA work in #8602). This PR makes the writer declare what it writes: standalone BMP saves of RGBA images now use a 108-byte BITMAPV4HEADER with BI_BITFIELDS compression and RGBA channel masks (R=0x00FF0000, G=0x0000FF00, B=0x000000FF, A=0xFF000000), so the alpha channel survives a save/reload round trip and is discoverable by any V4-aware reader.
Changes proposed in this pull request:
Compatibility tradeoff (explicit): this changes the default on-disk header for RGBA BMP saves. V4 headers date to the Windows 95/NT4 era and are widely supported, but a minimal decoder that only understands 40-byte headers could reject such files; I have not tested third-party decoders exhaustively. Would an opt-in encoder option be preferable to changing the default?