From 21e5d5d082dfe47fda2779e10d446e6593c04dd8 Mon Sep 17 00:00:00 2001 From: Andrew Murray Date: Wed, 14 Feb 2024 09:17:22 +1100 Subject: [PATCH] Use palette when loading --- Tests/test_file_ico.py | 11 +++++++++++ src/PIL/IcoImagePlugin.py | 2 ++ 2 files changed, 13 insertions(+) diff --git a/Tests/test_file_ico.py b/Tests/test_file_ico.py index 65f090931b4..e75561f6978 100644 --- a/Tests/test_file_ico.py +++ b/Tests/test_file_ico.py @@ -38,6 +38,17 @@ def test_black_and_white() -> None: assert im.size == (16, 16) +def test_palette(tmp_path: Path) -> None: + temp_file = str(tmp_path / "temp.ico") + + im = Image.new("P", (16, 16)) + im.save(temp_file) + + with Image.open(temp_file) as reloaded: + assert reloaded.mode == "P" + assert reloaded.palette is not None + + def test_invalid_file() -> None: with open("Tests/images/flower.jpg", "rb") as fp: with pytest.raises(SyntaxError): diff --git a/src/PIL/IcoImagePlugin.py b/src/PIL/IcoImagePlugin.py index 1b22f8645dc..b558fdf3476 100644 --- a/src/PIL/IcoImagePlugin.py +++ b/src/PIL/IcoImagePlugin.py @@ -329,6 +329,8 @@ def load(self): self.im = im.im self.pyaccess = None self._mode = im.mode + if im.palette: + self.palette = im.palette if im.size != self.size: warnings.warn("Image was not the expected size")