From bf0e34b7f8b3f2d1d47e248e4710dfc0ba323f0e Mon Sep 17 00:00:00 2001 From: Anish Pentyala Date: Sat, 14 Nov 2026 19:41:46 -0800 Subject: [PATCH 1/4] Add release note for BMP RGBA alpha preservation Updated release notes for version 13.1.0 to include changes regarding BMP saving and zlib dependency. --- docs/releasenotes/13.1.0.rst | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/docs/releasenotes/13.1.0.rst b/docs/releasenotes/13.1.0.rst index 02412d8d333..526c67d2c44 100644 --- a/docs/releasenotes/13.1.0.rst +++ b/docs/releasenotes/13.1.0.rst @@ -131,3 +131,10 @@ not bundled into the wheels, so the system zlib was used at runtime instead, and Pillow 13.0.0 also failed to import on systems where ``libz.so.1`` was not on the default library search path, such as NixOS. Linux wheels no longer require a system zlib, and gain the speed of zlib-ng as macOS and Windows wheels already did. + +BMP saving preserves alpha channel +^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +RGBA images saved as BMP are now written with a BITMAPV4HEADER, bitfields +compression and channel masks, so the alpha channel is preserved when the file +is reloaded instead of being silently dropped. Other modes are unchanged. From 4ee76027f2f45e9bf1b19a5dcbe73230d08d4938 Mon Sep 17 00:00:00 2001 From: Anish Pentyala Date: Sat, 14 Nov 2026 19:42:38 -0800 Subject: [PATCH 2/4] Enhance BMP tests for RGBA and headers Added tests for RGBA handling in BMP format. --- Tests/test_file_bmp.py | 60 +++++++++++++++++++++++++++++++++++++++++- 1 file changed, 59 insertions(+), 1 deletion(-) diff --git a/Tests/test_file_bmp.py b/Tests/test_file_bmp.py index a758a4efbea..d0607d0e2ff 100644 --- a/Tests/test_file_bmp.py +++ b/Tests/test_file_bmp.py @@ -20,7 +20,7 @@ from pathlib import Path -@pytest.mark.parametrize("mode", ("1", "L", "P", "RGB")) +@pytest.mark.parametrize("mode", ("1", "L", "P", "RGB", "RGBA")) def test_sanity(mode: str, tmp_path: Path) -> None: outfile = tmp_path / "temp.bmp" @@ -35,6 +35,64 @@ def test_sanity(mode: str, tmp_path: Path) -> None: assert reloaded.get_format_mimetype() == "image/bmp" +def test_save_rgba_preserves_alpha(tmp_path: Path) -> None: + # The alpha channel should survive a save/reload round trip + outfile = tmp_path / "temp.bmp" + + im = hopper("RGBA") + im.save(outfile, "BMP") + + with Image.open(outfile) as reloaded: + reloaded.load() + assert reloaded.mode == "RGBA" + assert_image_equal(im, reloaded) + + +def test_save_rgba_v4_header(tmp_path: Path) -> None: + # RGBA images are saved with a BITMAPV4HEADER, bitfields compression + # and channel masks, so that readers can find the alpha channel + outfile = tmp_path / "temp.bmp" + + hopper("RGBA").save(outfile, "BMP") + + with open(outfile, "rb") as fp: + data = fp.read() + + assert _binary.i32le(data, 14) == 108 # BITMAPV4HEADER + assert _binary.i32le(data, 14 + 16) == 3 # BI_BITFIELDS compression + assert _binary.i32le(data, 14 + 40) == 0x00FF0000 # red mask + assert _binary.i32le(data, 14 + 44) == 0x0000FF00 # green mask + assert _binary.i32le(data, 14 + 48) == 0x000000FF # blue mask + assert _binary.i32le(data, 14 + 52) == 0xFF000000 # alpha mask + + +def test_save_rgb_info_header(tmp_path: Path) -> None: + # Other modes keep the plain BITMAPINFOHEADER + outfile = tmp_path / "temp.bmp" + + hopper("RGB").save(outfile, "BMP") + + with open(outfile, "rb") as fp: + data = fp.read() + + assert _binary.i32le(data, 14) == 40 # BITMAPINFOHEADER + assert _binary.i32le(data, 14 + 16) == 0 # BI_RGB compression + + +def test_save_rgba_dib_info_header(tmp_path: Path) -> None: + # DIB data keeps the plain BITMAPINFOHEADER even for RGBA, since it may be + # embedded in other formats (e.g. icons) that expect it + outfile = tmp_path / "temp.dib" + + hopper("RGBA").save(outfile, "DIB") + + with open(outfile, "rb") as fp: + data = fp.read() + + assert _binary.i32le(data, 0) == 40 # BITMAPINFOHEADER + assert _binary.i32le(data, 16) == 0 # BI_RGB compression + + def test_invalid_file() -> None: with open("Tests/images/flower.jpg", "rb") as fp: with pytest.raises(SyntaxError): From 55e677646cc2bdb4aa8260deca58e8ac9f41e4fd Mon Sep 17 00:00:00 2001 From: Anish Pentyala Date: Sat, 14 Nov 2026 19:43:09 -0800 Subject: [PATCH 3/4] Preserve alpha when saving RGBA as BMP (BITMAPV4HEADER + BI_BITFIELDS) Updated BMP header handling to support RGBA images with BITMAPV4HEADER and added channel masks. --- src/PIL/BmpImagePlugin.py | 22 +++++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) diff --git a/src/PIL/BmpImagePlugin.py b/src/PIL/BmpImagePlugin.py index cc69947ea54..1859c1d3c3c 100644 --- a/src/PIL/BmpImagePlugin.py +++ b/src/PIL/BmpImagePlugin.py @@ -451,7 +451,11 @@ def _save( ppm = tuple(int(x * 39.3701 + 0.5) for x in dpi) stride = ((im.size[0] * bits + 7) // 8 + 3) & (~3) - header = 40 # or 64 for OS/2 version 2 + # Use a BITMAPV4HEADER for RGBA images so that the alpha channel mask is + # stored and the alpha channel is preserved. This is only done for BMP + # files: DIB data embedded in other formats (e.g. icons) may be read by + # software that expects a plain BITMAPINFOHEADER. + header = 108 if im.mode == "RGBA" and bitmap_header else 40 image = stride * im.size[1] if im.mode == "1": @@ -486,7 +490,8 @@ def _save( + o32(im.size[1]) # height + o16(1) # planes + o16(bits) # depth - + o32(0) # compression (0=uncompressed) + # compression (0=uncompressed, 3=bitfields, used to store channel masks) + + o32(3 if header == 108 else 0) + o32(image) # size of bitmap + o32(ppm[0]) # resolution + o32(ppm[1]) # resolution @@ -494,7 +499,18 @@ def _save( + o32(colors) # colors important ) - fp.write(b"\0" * (header - 40)) # padding (for OS/2 format) + if header == 108: + # BITMAPV4HEADER: channel masks, LCS_sRGB color space, then padding + fp.write( + o32(0x00FF0000) # red mask + + o32(0x0000FF00) # green mask + + o32(0x000000FF) # blue mask + + o32(0xFF000000) # alpha mask + + o32(0x73524742) # LCS_sRGB color space + + b"\0" * (header - 40 - 20) # endpoints and gamma fields + ) + else: + fp.write(b"\0" * (header - 40)) # padding (for OS/2 format) if palette: fp.write(palette) From 57af48ec15a4a3ff7d7dbbe84b93312b0485f70e Mon Sep 17 00:00:00 2001 From: Andrew Murray Date: Wed, 18 Nov 2026 12:41:44 +1100 Subject: [PATCH 4/4] Simplify code --- Tests/test_file_bmp.py | 62 +-------------------------------------- src/PIL/BmpImagePlugin.py | 16 ++++------ 2 files changed, 6 insertions(+), 72 deletions(-) diff --git a/Tests/test_file_bmp.py b/Tests/test_file_bmp.py index d0607d0e2ff..7df0c3b610d 100644 --- a/Tests/test_file_bmp.py +++ b/Tests/test_file_bmp.py @@ -25,74 +25,14 @@ def test_sanity(mode: str, tmp_path: Path) -> None: outfile = tmp_path / "temp.bmp" im = hopper(mode) - im.save(outfile, "BMP") + im.save(outfile) with Image.open(outfile) as reloaded: - reloaded.load() - assert im.mode == reloaded.mode - assert im.size == reloaded.size assert reloaded.format == "BMP" assert reloaded.get_format_mimetype() == "image/bmp" - - -def test_save_rgba_preserves_alpha(tmp_path: Path) -> None: - # The alpha channel should survive a save/reload round trip - outfile = tmp_path / "temp.bmp" - - im = hopper("RGBA") - im.save(outfile, "BMP") - - with Image.open(outfile) as reloaded: - reloaded.load() - assert reloaded.mode == "RGBA" assert_image_equal(im, reloaded) -def test_save_rgba_v4_header(tmp_path: Path) -> None: - # RGBA images are saved with a BITMAPV4HEADER, bitfields compression - # and channel masks, so that readers can find the alpha channel - outfile = tmp_path / "temp.bmp" - - hopper("RGBA").save(outfile, "BMP") - - with open(outfile, "rb") as fp: - data = fp.read() - - assert _binary.i32le(data, 14) == 108 # BITMAPV4HEADER - assert _binary.i32le(data, 14 + 16) == 3 # BI_BITFIELDS compression - assert _binary.i32le(data, 14 + 40) == 0x00FF0000 # red mask - assert _binary.i32le(data, 14 + 44) == 0x0000FF00 # green mask - assert _binary.i32le(data, 14 + 48) == 0x000000FF # blue mask - assert _binary.i32le(data, 14 + 52) == 0xFF000000 # alpha mask - - -def test_save_rgb_info_header(tmp_path: Path) -> None: - # Other modes keep the plain BITMAPINFOHEADER - outfile = tmp_path / "temp.bmp" - - hopper("RGB").save(outfile, "BMP") - - with open(outfile, "rb") as fp: - data = fp.read() - - assert _binary.i32le(data, 14) == 40 # BITMAPINFOHEADER - assert _binary.i32le(data, 14 + 16) == 0 # BI_RGB compression - - -def test_save_rgba_dib_info_header(tmp_path: Path) -> None: - # DIB data keeps the plain BITMAPINFOHEADER even for RGBA, since it may be - # embedded in other formats (e.g. icons) that expect it - outfile = tmp_path / "temp.dib" - - hopper("RGBA").save(outfile, "DIB") - - with open(outfile, "rb") as fp: - data = fp.read() - - assert _binary.i32le(data, 0) == 40 # BITMAPINFOHEADER - assert _binary.i32le(data, 16) == 0 # BI_RGB compression - - def test_invalid_file() -> None: with open("Tests/images/flower.jpg", "rb") as fp: with pytest.raises(SyntaxError): diff --git a/src/PIL/BmpImagePlugin.py b/src/PIL/BmpImagePlugin.py index 1859c1d3c3c..6de6676a38c 100644 --- a/src/PIL/BmpImagePlugin.py +++ b/src/PIL/BmpImagePlugin.py @@ -451,10 +451,7 @@ def _save( ppm = tuple(int(x * 39.3701 + 0.5) for x in dpi) stride = ((im.size[0] * bits + 7) // 8 + 3) & (~3) - # Use a BITMAPV4HEADER for RGBA images so that the alpha channel mask is - # stored and the alpha channel is preserved. This is only done for BMP - # files: DIB data embedded in other formats (e.g. icons) may be read by - # software that expects a plain BITMAPINFOHEADER. + # Use a BITMAPV4HEADER for RGBA BMP images (not DIB) header = 108 if im.mode == "RGBA" and bitmap_header else 40 image = stride * im.size[1] @@ -490,8 +487,7 @@ def _save( + o32(im.size[1]) # height + o16(1) # planes + o16(bits) # depth - # compression (0=uncompressed, 3=bitfields, used to store channel masks) - + o32(3 if header == 108 else 0) + + o32(BmpImageFile.COMPRESSIONS["BITFIELDS" if header == 108 else "RAW"]) + o32(image) # size of bitmap + o32(ppm[0]) # resolution + o32(ppm[1]) # resolution @@ -500,17 +496,15 @@ def _save( ) if header == 108: - # BITMAPV4HEADER: channel masks, LCS_sRGB color space, then padding + # BITMAPV4HEADER fp.write( o32(0x00FF0000) # red mask + o32(0x0000FF00) # green mask + o32(0x000000FF) # blue mask + o32(0xFF000000) # alpha mask - + o32(0x73524742) # LCS_sRGB color space - + b"\0" * (header - 40 - 20) # endpoints and gamma fields + + b"sRGB"[::-1] # LCS_sRGB color space + + b"\0" * 48 # endpoints and gamma ) - else: - fp.write(b"\0" * (header - 40)) # padding (for OS/2 format) if palette: fp.write(palette)