From 317286d260488e1e60a3d25b4d6c3fce83ba12f8 Mon Sep 17 00:00:00 2001 From: Andrew Murray Date: Wed, 22 Jun 2022 17:27:49 +1000 Subject: [PATCH] Pad palette to 768 bytes --- Tests/test_file_pcx.py | 5 +++++ src/PIL/PcxImagePlugin.py | 4 +++- 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/Tests/test_file_pcx.py b/Tests/test_file_pcx.py index 61e33a57bb4..ba6663cd3ba 100644 --- a/Tests/test_file_pcx.py +++ b/Tests/test_file_pcx.py @@ -20,6 +20,11 @@ def test_sanity(tmp_path): for mode in ("1", "L", "P", "RGB"): _roundtrip(tmp_path, hopper(mode)) + # Test a palette with less than 256 colors + im = Image.new("P", (1, 1)) + im.putpalette((255, 0, 0)) + _roundtrip(tmp_path, im) + # Test an unsupported mode f = str(tmp_path / "temp.pcx") im = hopper("RGBA") diff --git a/src/PIL/PcxImagePlugin.py b/src/PIL/PcxImagePlugin.py index d2e166bdd77..841c18a2200 100644 --- a/src/PIL/PcxImagePlugin.py +++ b/src/PIL/PcxImagePlugin.py @@ -198,7 +198,9 @@ def _save(im, fp, filename): if im.mode == "P": # colour palette fp.write(o8(12)) - fp.write(im.im.getpalette("RGB", "RGB")) # 768 bytes + palette = im.im.getpalette("RGB", "RGB") + palette += b"\x00" * (768 - len(palette)) + fp.write(palette) # 768 bytes elif im.mode == "L": # greyscale palette fp.write(o8(12))