From 09315f263bc69ae76340679c1f34e83591ab28c8 Mon Sep 17 00:00:00 2001 From: David Schmidt Date: Thu, 21 Mar 2013 18:16:00 +0100 Subject: [PATCH] fixing save of transparency palette png-images --- PIL/PngImagePlugin.py | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/PIL/PngImagePlugin.py b/PIL/PngImagePlugin.py index ace71d21d..bc1aff262 100644 --- a/PIL/PngImagePlugin.py +++ b/PIL/PngImagePlugin.py @@ -550,11 +550,14 @@ def _save(im, fp, filename, chunk=putchunk, check=0): if "transparency" in im.encoderinfo: if im.mode == "P": + # limit to actual palette size + alpha_bytes = 2**bits if isinstance(im.encoderinfo["transparency"], bytes): - chunk(fp, b"tRNS", b'\xFF' + im.encoderinfo["transparency"] + b'\0') + chunk(fp, b"tRNS", im.encoderinfo["transparency"][:alpha_bytes]) else: - transparency = max(0, min(255, im.encoderinfo["transparency"])) - chunk(fp, b"tRNS", b'\xFF' + transparency + b'\0') + transparency = max(0, min(255, im.encoderinfo["transparency"])) + alpha = b'\xFF' * transparency + b'\0' + 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))