From e317a729f31c83c5d0d43e517507774757a5686f Mon Sep 17 00:00:00 2001 From: Justin Wilson Date: Fri, 22 May 2015 16:37:50 -0600 Subject: [PATCH 1/2] Setting transparency value to 0 when the tRNS contains only null byte(s) --- PIL/PngImagePlugin.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/PIL/PngImagePlugin.py b/PIL/PngImagePlugin.py index 398a01f33..4b677b7cb 100644 --- a/PIL/PngImagePlugin.py +++ b/PIL/PngImagePlugin.py @@ -71,6 +71,7 @@ _MODES = { _simple_palette = re.compile(b'^\xff+\x00\xff*$') +_null_palette = re.compile(b'^\x00*$') # Maximum decompressed size for a iTXt or zTXt chunk. # Eliminates decompression bombs where compressed chunks can expand 1000x @@ -350,6 +351,8 @@ class PngStream(ChunkStream): i = s.find(b"\0") if i >= 0: self.im_info["transparency"] = i + elif _null_palette.match(s): + self.im_info["transparency"] = 0 else: self.im_info["transparency"] = s elif self.im_mode == "L": From e33b5c635551f7a5434d06b4f2396e42f468f64a Mon Sep 17 00:00:00 2001 From: Justin Wilson Date: Wed, 27 May 2015 09:45:27 -0600 Subject: [PATCH 2/2] Added test to check that png with null tRNS value defaults to a zero transparency value. --- Tests/images/tRNS_null_1x1.png | Bin 0 -> 96 bytes Tests/test_file_png.py | 7 +++++++ 2 files changed, 7 insertions(+) create mode 100644 Tests/images/tRNS_null_1x1.png diff --git a/Tests/images/tRNS_null_1x1.png b/Tests/images/tRNS_null_1x1.png new file mode 100644 index 0000000000000000000000000000000000000000..976eae9390a4816aeee265e5014468cd9a6d7464 GIT binary patch literal 96 zcmeAS@N?(olHy`uVBq!ia0vp^j3CU&3?x-=hn)ga%mF?juK#@*VoWXSL2@NQe!&b5 n&u*jvIb5DDjv*Cul9PaJHU?%h^O_Yv7K5j&pUXO@geEQkXtESH literal 0 HcmV?d00001 diff --git a/Tests/test_file_png.py b/Tests/test_file_png.py index b3169ed25..f438e24cc 100644 --- a/Tests/test_file_png.py +++ b/Tests/test_file_png.py @@ -354,6 +354,13 @@ class TestFilePng(PillowTestCase): self.assert_image_equal(im2.convert('RGBA'), im.convert('RGBA')) + def test_trns_null(self): + # Check reading images with null tRNS value, issue #1239 + test_file = "Tests/images/tRNS_null_1x1.png" + im = Image.open(test_file) + + self.assertEqual(im.info["transparency"], 0) + def test_save_icc_profile_none(self): # check saving files with an ICC profile set to None (omit profile) in_file = "Tests/images/icc_profile_none.png"