From c773688f37da26d997c9a70c5e53c4aee7d64b82 Mon Sep 17 00:00:00 2001 From: Oliver Tonnhofer Date: Tue, 19 Apr 2016 09:52:01 +0200 Subject: [PATCH] improve simple palette detection for PNG - reverts change #1239 which could remove transparency from valid images (see test_save_p_transparent_black test case) - improves simple palette detection to handle images from #1238 --- PIL/PngImagePlugin.py | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/PIL/PngImagePlugin.py b/PIL/PngImagePlugin.py index cc58fbbc4..7734fdeda 100644 --- a/PIL/PngImagePlugin.py +++ b/PIL/PngImagePlugin.py @@ -74,8 +74,7 @@ _MODES = { } -_simple_palette = re.compile(b'^\xff+\x00\xff*$') -_null_palette = re.compile(b'^\x00*$') +_simple_palette = re.compile(b'^\xff*\x00\xff*$') # Maximum decompressed size for a iTXt or zTXt chunk. # Eliminates decompression bombs where compressed chunks can expand 1000x @@ -118,7 +117,7 @@ class ChunkStream(object): cid = s[4:] pos = self.fp.tell() length = i32(s) - + if not is_cid(cid): raise SyntaxError("broken PNG file (chunk %s)" % repr(cid)) @@ -359,12 +358,14 @@ class PngStream(ChunkStream): s = ImageFile._safe_read(self.fp, length) if self.im_mode == "P": if _simple_palette.match(s): + # tRNS contains only one full-transparent entry, + # other entries are full opaque i = s.find(b"\0") if i >= 0: self.im_info["transparency"] = i - elif _null_palette.match(s): - self.im_info["transparency"] = 0 else: + # otherwise, we have a byte string with one alpha value + # for each palette entry self.im_info["transparency"] = s elif self.im_mode == "L": self.im_info["transparency"] = i16(s)