From 6a450e303d2c3a03dcd7900e748338d3ed6c5681 Mon Sep 17 00:00:00 2001 From: Oliver Tonnhofer Date: Mon, 11 Mar 2013 20:58:54 +0100 Subject: [PATCH] reduce PNG palette size for images <255 colors limit color and trancparency palette to 2^bits entries, when a PNG is saved with the 'bits' PNG encoder option --- PIL/PngImagePlugin.py | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/PIL/PngImagePlugin.py b/PIL/PngImagePlugin.py index 96e4431fc..d556360a9 100644 --- a/PIL/PngImagePlugin.py +++ b/PIL/PngImagePlugin.py @@ -545,12 +545,16 @@ def _save(im, fp, filename, chunk=putchunk, check=0): b'\0') # 12: interlace flag if im.mode == "P": - chunk(fp, b"PLTE", im.im.getpalette("RGB")) + palette_bytes = (2 ** bits) * 3 + chunk(fp, b"PLTE", im.im.getpalette("RGB")[:palette_bytes]) if "transparency" in im.encoderinfo: if im.mode == "P": transparency = max(0, min(255, im.encoderinfo["transparency"])) - chunk(fp, b"tRNS", b'\xFF' * transparency + b'\0') + alpha = b'\xFF' * transparency + b'\0' + # limit to actual palette size + alpha_bytes = 2**bits + chunk(fp, b"tRNS", alpha[:alpha_bytes]) elif im.mode == "L": transparency = max(0, min(65535, im.encoderinfo["transparency"])) chunk(fp, b"tRNS", o16(transparency)) @@ -562,7 +566,8 @@ def _save(im, fp, filename, chunk=putchunk, check=0): else: if im.mode == "P" and im.im.getpalettemode() == "RGBA": alpha = im.im.getpalette("RGBA", "A") - chunk(fp, "tRNS", alpha) + alpha_bytes = 2**bits + chunk(fp, b"tRNS", alpha[:alpha_bytes]) if 0: # FIXME: to be supported some day