From c4cd036dd8f13adbc20b7289049c878d6aefeb71 Mon Sep 17 00:00:00 2001 From: Alexander Date: Thu, 7 Dec 2017 18:43:22 +0300 Subject: [PATCH 1/3] Fix count of BITSPERSAMPLE items in broken TIFF files --- PIL/TiffImagePlugin.py | 21 ++++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) diff --git a/PIL/TiffImagePlugin.py b/PIL/TiffImagePlugin.py index 07fb97d14..7f31389eb 100644 --- a/PIL/TiffImagePlugin.py +++ b/PIL/TiffImagePlugin.py @@ -1186,12 +1186,23 @@ class TiffImageFile(ImageFile.ImageFile): # for more exotic images. sampleFormat = (1,) + bps_tuple = self.tag_v2.get(BITSPERSAMPLE, (1,)) + extra_tuple = self.tag_v2.get(EXTRASAMPLES, ()) + if photo in (2, 6, 8): # RGB, YCbCr, LAB + bps_count = 3 + elif photo == 5: + bps_count = 4 + else: + bps_count = 1 + bps_count += len(extra_tuple) + # Some files have only one value in bps_tuple, + # while should have more. Fix it + if bps_count > len(bps_tuple) and len(bps_tuple) == 1: + bps_tuple = bps_tuple * bps_count + # mode: check photometric interpretation and bits per pixel - key = ( - self.tag_v2.prefix, photo, sampleFormat, fillorder, - self.tag_v2.get(BITSPERSAMPLE, (1,)), - self.tag_v2.get(EXTRASAMPLES, ()) - ) + key = (self.tag_v2.prefix, photo, sampleFormat, fillorder, + bps_tuple, extra_tuple) if DEBUG: print("format key:", key) try: From f0ae36080ce90bfed37021672da9158438553063 Mon Sep 17 00:00:00 2001 From: Alexander Date: Fri, 8 Dec 2017 15:37:21 +0300 Subject: [PATCH 2/3] fix comments --- PIL/TiffImagePlugin.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/PIL/TiffImagePlugin.py b/PIL/TiffImagePlugin.py index 7f31389eb..d3be09cb0 100644 --- a/PIL/TiffImagePlugin.py +++ b/PIL/TiffImagePlugin.py @@ -1188,9 +1188,9 @@ class TiffImageFile(ImageFile.ImageFile): bps_tuple = self.tag_v2.get(BITSPERSAMPLE, (1,)) extra_tuple = self.tag_v2.get(EXTRASAMPLES, ()) - if photo in (2, 6, 8): # RGB, YCbCr, LAB + if photo in (2, 6, 8): # RGB, YCbCr, LAB bps_count = 3 - elif photo == 5: + elif photo == 5: # CMYK bps_count = 4 else: bps_count = 1 From ce151bcd72bc7800698c2721a44a138640857d64 Mon Sep 17 00:00:00 2001 From: Alexander Date: Fri, 8 Dec 2017 17:32:39 +0300 Subject: [PATCH 3/3] add test for this case --- Tests/images/tiff_wrong_bits_per_sample.tiff | Bin 0 -> 11184 bytes Tests/test_file_tiff.py | 8 ++++++++ 2 files changed, 8 insertions(+) create mode 100644 Tests/images/tiff_wrong_bits_per_sample.tiff diff --git a/Tests/images/tiff_wrong_bits_per_sample.tiff b/Tests/images/tiff_wrong_bits_per_sample.tiff new file mode 100644 index 0000000000000000000000000000000000000000..554d4b35103ac38df683c9251e32a05db9af4050 GIT binary patch literal 11184 zcmeI2dvH|M9mhd!r?vms8QcD$LbBPsd4@a)p-OoPP8k)dRX|gLfuXGq!RbufnGVxw z>$EzBgsc)Z3;^MK2YaYV_04zh&yJ>iBoizt!mTyl)%* zThRZ$asE5#|G+qY8~Q&qj`x3e%9I}&{VI3VEvrMWU4uT)-3{q~Q}KKJkK^F~zxlUk zPuaJ2>^O2ewR!UnXtUB!(S{Sh=W+AAV9RTF;68iQz4q)ma=VCd`ftX+JFgn9Ob_pY`-^zbwG6)T@Y{~>!?+EV+xd5fxlvS{gv)YR0x^z`)H zl+={N85tQn=g*(N5s-I`7c>wE-0{ia;G%cmJ$UG~ox9}CxAw`-UAtxX?lo=&%f}Jy!g^9XkV6>UVc^nhR^%?b z`S?yN);d>WEXiUq6`cgOIT9C_0cHqoLP93A0PHv(n*)~(wj{=ikGUjJ3` z`LBrE+b`YDUg>fMq@n5Z(~;WA(;L|S6?pmwuE~`v;J|%7-ac`A0;QjXrr|W7F~7JhW+wi=X36;ku}(6nX5iP2zO*D)l&h z;P6Rjmq+ZEd&JSxE4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iff.py b/Tests/test_file_tiff.py index 3ad8b2bd1..6a3166de4 100644 --- a/Tests/test_file_tiff.py +++ b/Tests/test_file_tiff.py @@ -59,6 +59,14 @@ class TestFileTiff(PillowTestCase): self.assertEqual(im.tile, [('tiff_lzw', (0, 0, 100, 40), 50, 'RGBa;16B')]) im.load() + def test_wrong_bits_per_sample(self): + im = Image.open("Tests/images/tiff_wrong_bits_per_sample.tiff") + + self.assertEqual(im.mode, "RGBA") + self.assertEqual(im.size, (52, 53)) + self.assertEqual(im.tile, [('raw', (0, 0, 52, 53), 160, ('RGBA', 0, 1))]) + im.load() + def test_gimp_tiff(self): # Read TIFF JPEG images from GIMP [@PIL168]