diff --git a/Tests/test_file_bmp.py b/Tests/test_file_bmp.py index e5a66de1fe3..76d223fb87f 100644 --- a/Tests/test_file_bmp.py +++ b/Tests/test_file_bmp.py @@ -90,6 +90,51 @@ def test_empty_palette(tmp_path: Path) -> None: assert_image_equal(im.convert("1"), reloaded) +def test_save_black_and_white_palette() -> None: + im = Image.new("P", (4, 1)) + im.putpalette((0, 0, 0, 255, 255, 255)) + im.putdata((0, 1, 1, 0)) + + out = io.BytesIO() + im.save(out, "BMP") + + with Image.open(out) as reloaded: + assert_image_equal(reloaded.convert("RGB"), im.convert("RGB")) + + +@pytest.mark.parametrize( + "bits, colors, data, mode, expected", + ( + (1, (0, 1, 2, 3), b"\x50", "P", (0, 1, 0, 1)), + (4, (0, 255), b"\x01\x10", "P", (0, 255, 255, 0)), + (4, tuple(range(16)), b"\x12\x34", "P", (1, 2, 3, 4)), + (8, (0, 255), b"\x00\x01\x01\x00", "1", (0, 255, 255, 0)), + ), +) +def test_grayscale_palette_bit_depth( + bits: int, + colors: tuple[int, ...], + data: bytes, + mode: str, + expected: tuple[int, ...], +) -> None: + fp = io.BytesIO( + o32(40) # header size + + o32(4) # width + + o32(1) # height + + o16(1) # planes + + o16(bits) + + b"\x00" * 16 + + o32(len(colors)) + + o32(0) + + b"".join(bytes((color, color, color, 0)) for color in colors) + + data.ljust(4, b"\x00") + ) + with Image.open(fp) as im: + assert im.mode == mode + assert im.convert("L").tobytes() == bytes(expected) + + def test_save_too_large(tmp_path: Path) -> None: outfile = tmp_path / "temp.bmp" with Image.new("RGB", (1, 1)) as im: diff --git a/docs/releasenotes/13.1.0.rst b/docs/releasenotes/13.1.0.rst index ad1ff23a499..eee21e22655 100644 --- a/docs/releasenotes/13.1.0.rst +++ b/docs/releasenotes/13.1.0.rst @@ -256,6 +256,13 @@ 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. +Reading BMP images with grayscale palettes +^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +Uncompressed BMP images with a grayscale palette were read incorrectly if the bit depth +of the file did not match the mode chosen for the palette, such as an 8-bit image with +only black and white in the palette. This has been fixed. + Kerning in the basic layout engine ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ diff --git a/src/PIL/BmpImagePlugin.py b/src/PIL/BmpImagePlugin.py index 41749da526f..cbe8bdd498f 100644 --- a/src/PIL/BmpImagePlugin.py +++ b/src/PIL/BmpImagePlugin.py @@ -285,10 +285,18 @@ def _bitmap(self, header: int = 0, offset: int = 0) -> None: padding = file_info["palette_padding"] palette = read(padding * file_info["colors"]) - if _grayscale_palette(file_info["colors"], padding).startswith(palette): + # 4-bit indices can only be unpacked as P, and 1-bit indices as 1 or P + mode = "1" if file_info["colors"] <= 2 else "L" + if ( + decoder_name == "bmp_rle" + or file_info["bits"] == 8 + or (file_info["bits"] == 1 and mode == "1") + ) and _grayscale_palette(file_info["colors"], padding).startswith( + palette + ): # ------- If all colors are gray, white or black, ditch palette - self._mode = "1" if file_info["colors"] <= 2 else "L" - raw_mode = self.mode + self._mode = mode + raw_mode = "1;8" if mode == "1" and file_info["bits"] == 8 else mode else: self._mode = "P" self.palette = ImagePalette.raw(