diff --git a/Tests/images/rgb8.dds b/Tests/images/rgb8.dds new file mode 100644 index 000000000..8193e8e5a Binary files /dev/null and b/Tests/images/rgb8.dds differ diff --git a/Tests/test_file_dds.py b/Tests/test_file_dds.py index 72bb2df7b..335c4c2de 100644 --- a/Tests/test_file_dds.py +++ b/Tests/test_file_dds.py @@ -340,6 +340,12 @@ def test_open(mode, test_file): assert_image_equal_tofile(im, test_file.replace(".dds", ".png")) +def test_open_rgb8(): + with Image.open("Tests/images/rgb8.dds") as im: + assert im.mode == "L" + assert_image_equal_tofile(im, "Tests/images/mode-l.png") + + @pytest.mark.parametrize( ("mode", "test_file"), [ diff --git a/src/PIL/DdsImagePlugin.py b/src/PIL/DdsImagePlugin.py index b27781f3c..5c4abfba3 100644 --- a/src/PIL/DdsImagePlugin.py +++ b/src/PIL/DdsImagePlugin.py @@ -353,21 +353,22 @@ class DdsImageFile(ImageFile.ImageFile): if pfflags & DDPF.RGB: # Texture contains uncompressed RGB data masks = {mask: ["R", "G", "B", "A"][i] for i, mask in enumerate(masks)} - if bitcount == 24: + if bitcount == 8: + self._mode = "L" + elif bitcount == 24: self._mode = "RGB" - rawmode = masks[0x00FF0000] + masks[0x0000FF00] + masks[0x000000FF] + rawmode = masks[0x000000FF] + masks[0x0000FF00] + masks[0x00FF0000] elif bitcount == 32 and pfflags & DDPF.ALPHAPIXELS: self._mode = "RGBA" rawmode = ( - masks[0xFF000000] - + masks[0x00FF0000] + masks[0x000000FF] + masks[0x0000FF00] - + masks[0x000000FF] + + masks[0x00FF0000] + + masks[0xFF000000] ) else: msg = f"Unsupported bitcount {bitcount} for {pfflags}" raise OSError(msg) - rawmode = rawmode[::-1] elif pfflags & DDPF.LUMINANCE: if bitcount == 8: self._mode = "L"