From 1ff2b6630a3d2e05a1f834b65ce2903689a2992f Mon Sep 17 00:00:00 2001 From: David Schmidt Date: Thu, 21 Mar 2013 16:54:04 +0100 Subject: [PATCH 1/5] fixed bug with png-images with transparency palette --- PIL/PngImagePlugin.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/PIL/PngImagePlugin.py b/PIL/PngImagePlugin.py index d556360a9..ace71d21d 100644 --- a/PIL/PngImagePlugin.py +++ b/PIL/PngImagePlugin.py @@ -550,11 +550,11 @@ def _save(im, fp, filename, chunk=putchunk, check=0): if "transparency" in im.encoderinfo: if im.mode == "P": - transparency = max(0, min(255, im.encoderinfo["transparency"])) - alpha = b'\xFF' * transparency + b'\0' - # limit to actual palette size - alpha_bytes = 2**bits - chunk(fp, b"tRNS", alpha[:alpha_bytes]) + if isinstance(im.encoderinfo["transparency"], bytes): + chunk(fp, b"tRNS", b'\xFF' + im.encoderinfo["transparency"] + b'\0') + else: + transparency = max(0, min(255, im.encoderinfo["transparency"])) + chunk(fp, b"tRNS", b'\xFF' + transparency + b'\0') elif im.mode == "L": transparency = max(0, min(65535, im.encoderinfo["transparency"])) chunk(fp, b"tRNS", o16(transparency)) From 09315f263bc69ae76340679c1f34e83591ab28c8 Mon Sep 17 00:00:00 2001 From: David Schmidt Date: Thu, 21 Mar 2013 18:16:00 +0100 Subject: [PATCH 2/5] 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)) From 1a40613ca5ad506a32efdb008793eaaca54f8bcb Mon Sep 17 00:00:00 2001 From: David Schmidt Date: Thu, 21 Mar 2013 18:43:22 +0100 Subject: [PATCH 3/5] fix indention --- PIL/PngImagePlugin.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/PIL/PngImagePlugin.py b/PIL/PngImagePlugin.py index bc1aff262..d1f7ac974 100644 --- a/PIL/PngImagePlugin.py +++ b/PIL/PngImagePlugin.py @@ -555,9 +555,9 @@ def _save(im, fp, filename, chunk=putchunk, check=0): if isinstance(im.encoderinfo["transparency"], bytes): chunk(fp, b"tRNS", im.encoderinfo["transparency"][:alpha_bytes]) else: - transparency = max(0, min(255, im.encoderinfo["transparency"])) - alpha = b'\xFF' * transparency + b'\0' - chunk(fp, b"tRNS", alpha[:alpha_bytes]) + 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)) From ecd55629bef7234332b9fa6ca93de311819bf7f2 Mon Sep 17 00:00:00 2001 From: David Schmidt Date: Thu, 21 Mar 2013 18:47:37 +0100 Subject: [PATCH 4/5] fix indention --- PIL/PngImagePlugin.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/PIL/PngImagePlugin.py b/PIL/PngImagePlugin.py index d1f7ac974..65c0759e5 100644 --- a/PIL/PngImagePlugin.py +++ b/PIL/PngImagePlugin.py @@ -550,7 +550,7 @@ def _save(im, fp, filename, chunk=putchunk, check=0): if "transparency" in im.encoderinfo: if im.mode == "P": - # limit to actual palette size + # limit to actual palette size alpha_bytes = 2**bits if isinstance(im.encoderinfo["transparency"], bytes): chunk(fp, b"tRNS", im.encoderinfo["transparency"][:alpha_bytes]) From 511adfacf7780054c9c31639c527f0c33bf32136 Mon Sep 17 00:00:00 2001 From: David Schmidt Date: Thu, 21 Mar 2013 21:00:25 +0100 Subject: [PATCH 5/5] fix png decode tRNS pattern --- PIL/PngImagePlugin.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/PIL/PngImagePlugin.py b/PIL/PngImagePlugin.py index 65c0759e5..7ed018878 100644 --- a/PIL/PngImagePlugin.py +++ b/PIL/PngImagePlugin.py @@ -70,7 +70,7 @@ _MODES = { } -_simple_palette = re.compile(b'^\xff+\x00+$') +_simple_palette = re.compile(b'^\xff+\x00\xff*$') # -------------------------------------------------------------------- # Support classes. Suitable for PNG and related formats like MNG etc.