From 55b1accb42a5b2d4e1372546629ff06024863915 Mon Sep 17 00:00:00 2001 From: hugovk Date: Sat, 17 May 2014 19:08:08 +0300 Subject: [PATCH 1/5] Add (failing) test for #664 --- Tests/test_image_convert.py | 29 +++++++++++++++++++++-------- 1 file changed, 21 insertions(+), 8 deletions(-) diff --git a/Tests/test_image_convert.py b/Tests/test_image_convert.py index 4d40e43b2..6f170afe2 100644 --- a/Tests/test_image_convert.py +++ b/Tests/test_image_convert.py @@ -45,7 +45,7 @@ def test_16bit(): def test_16bit_workaround(): im = Image.open('Tests/images/16bit.cropped.tif') _test_float_conversion(im.convert('I')) - + def test_rgba_p(): im = lena('RGBA') im.putalpha(lena('L')) @@ -54,8 +54,8 @@ def test_rgba_p(): comparable = converted.convert('RGBA') assert_image_similar(im, comparable, 20) - -def test_trns_p(): + +def test_trns_p(): im = lena('P') im.info['transparency']=0 @@ -69,13 +69,26 @@ def test_trns_p(): rgb = im.convert('RGB') assert_equal(rgb.info['transparency'], (0,0,0)) # undone assert_no_exception(lambda: rgb.save(f)) - + + +def test_trns_p_rgba(): + # Arrange + im = lena('P') + im.info['transparency'] = 128 + + # Act + rgba = im.convert('RGBA') + + # Assert + assert_false('transparency' in rgba.info) + + def test_trns_l(): im = lena('L') im.info['transparency'] = 128 f = tempfile('temp.png') - + rgb = im.convert('RGB') assert_equal(rgb.info['transparency'], (128,128,128)) # undone assert_no_exception(lambda: rgb.save(f)) @@ -89,13 +102,13 @@ def test_trns_l(): assert_false('transparency' in p.info) assert_no_exception(lambda: p.save(f)) - + def test_trns_RGB(): im = lena('RGB') im.info['transparency'] = im.getpixel((0,0)) f = tempfile('temp.png') - + l = im.convert('L') assert_equal(l.info['transparency'], l.getpixel((0,0))) # undone assert_no_exception(lambda: l.save(f)) @@ -103,7 +116,7 @@ def test_trns_RGB(): p = im.convert('P') assert_true('transparency' in p.info) assert_no_exception(lambda: p.save(f)) - + p = assert_warning(UserWarning, lambda: im.convert('P', palette = Image.ADAPTIVE)) assert_false('transparency' in p.info) From ca7608f1d5a0d4322d185da6607fa1db2b418c0f Mon Sep 17 00:00:00 2001 From: hugovk Date: Sat, 17 May 2014 20:36:17 +0300 Subject: [PATCH 2/5] Possible fix for #664 --- PIL/Image.py | 41 ++++++++++++++++++++++------------------- 1 file changed, 22 insertions(+), 19 deletions(-) diff --git a/PIL/Image.py b/PIL/Image.py index 333397701..ec25f1027 100644 --- a/PIL/Image.py +++ b/PIL/Image.py @@ -101,7 +101,7 @@ import collections import numbers # works everywhere, win for pypy, not cpython -USE_CFFI_ACCESS = hasattr(sys, 'pypy_version_info') +USE_CFFI_ACCESS = hasattr(sys, 'pypy_version_info') try: import cffi HAS_CFFI=True @@ -233,7 +233,7 @@ _MODE_CONV = { "CMYK": ('|u1', 4), "YCbCr": ('|u1', 3), "LAB": ('|u1', 3), # UNDONE - unsigned |u1i1i1 - # I;16 == I;16L, and I;32 == I;32L + # I;16 == I;16L, and I;32 == I;32L "I;16": ('u2', None), "I;16L": (' 8bit images. + # a gamma function point transform on > 8bit images. scale, offset = _getscaleoffset(lut) return self._new(self.im.point_transform(scale, offset)) # for other modes, convert the function to a table @@ -1420,8 +1423,8 @@ class Image: self._copy() self.pyaccess = None self.load() - - if self.pyaccess: + + if self.pyaccess: return self.pyaccess.putpixel(xy,value) return self.im.putpixel(xy, value) From a59bc40c38661c92748d8be93ca2e03bdb01f613 Mon Sep 17 00:00:00 2001 From: hugovk Date: Sat, 17 May 2014 23:33:50 +0300 Subject: [PATCH 3/5] flake8 --- Tests/test_image_convert.py | 33 +++++++++++++++++++-------------- 1 file changed, 19 insertions(+), 14 deletions(-) diff --git a/Tests/test_image_convert.py b/Tests/test_image_convert.py index 6f170afe2..6a39b0e3b 100644 --- a/Tests/test_image_convert.py +++ b/Tests/test_image_convert.py @@ -2,6 +2,7 @@ from tester import * from PIL import Image + def test_sanity(): def convert(im, mode): @@ -16,6 +17,7 @@ def test_sanity(): for mode in modes: yield_test(convert, im, mode) + def test_default(): im = lena("P") @@ -26,26 +28,29 @@ def test_default(): assert_image(im, "RGB", im.size) - # ref https://github.com/python-imaging/Pillow/issues/274 def _test_float_conversion(im): - orig = im.getpixel((5,5)) - converted = im.convert('F').getpixel((5,5)) + orig = im.getpixel((5, 5)) + converted = im.convert('F').getpixel((5, 5)) assert_equal(orig, converted) + def test_8bit(): im = Image.open('Images/lena.jpg') _test_float_conversion(im.convert('L')) + def test_16bit(): im = Image.open('Tests/images/16bit.cropped.tif') _test_float_conversion(im) + def test_16bit_workaround(): im = Image.open('Tests/images/16bit.cropped.tif') _test_float_conversion(im.convert('I')) + def test_rgba_p(): im = lena('RGBA') im.putalpha(lena('L')) @@ -55,22 +60,24 @@ def test_rgba_p(): assert_image_similar(im, comparable, 20) + def test_trns_p(): im = lena('P') - im.info['transparency']=0 + im.info['transparency'] = 0 f = tempfile('temp.png') l = im.convert('L') - assert_equal(l.info['transparency'], 0) # undone + assert_equal(l.info['transparency'], 0) # undone assert_no_exception(lambda: l.save(f)) - rgb = im.convert('RGB') - assert_equal(rgb.info['transparency'], (0,0,0)) # undone + assert_equal(rgb.info['transparency'], (0, 0, 0)) # undone assert_no_exception(lambda: rgb.save(f)) +# ref https://github.com/python-imaging/Pillow/issues/664 + def test_trns_p_rgba(): # Arrange im = lena('P') @@ -90,7 +97,7 @@ def test_trns_l(): f = tempfile('temp.png') rgb = im.convert('RGB') - assert_equal(rgb.info['transparency'], (128,128,128)) # undone + assert_equal(rgb.info['transparency'], (128, 128, 128)) # undone assert_no_exception(lambda: rgb.save(f)) p = im.convert('P') @@ -98,19 +105,19 @@ def test_trns_l(): assert_no_exception(lambda: p.save(f)) p = assert_warning(UserWarning, - lambda: im.convert('P', palette = Image.ADAPTIVE)) + lambda: im.convert('P', palette=Image.ADAPTIVE)) assert_false('transparency' in p.info) assert_no_exception(lambda: p.save(f)) def test_trns_RGB(): im = lena('RGB') - im.info['transparency'] = im.getpixel((0,0)) + im.info['transparency'] = im.getpixel((0, 0)) f = tempfile('temp.png') l = im.convert('L') - assert_equal(l.info['transparency'], l.getpixel((0,0))) # undone + assert_equal(l.info['transparency'], l.getpixel((0, 0))) # undone assert_no_exception(lambda: l.save(f)) p = im.convert('P') @@ -118,8 +125,6 @@ def test_trns_RGB(): assert_no_exception(lambda: p.save(f)) p = assert_warning(UserWarning, - lambda: im.convert('P', palette = Image.ADAPTIVE)) + lambda: im.convert('P', palette=Image.ADAPTIVE)) assert_false('transparency' in p.info) assert_no_exception(lambda: p.save(f)) - - From 0938145e57e6d4e838f02495da80e03fd2a4cbeb Mon Sep 17 00:00:00 2001 From: hugovk Date: Mon, 19 May 2014 10:04:56 +0300 Subject: [PATCH 4/5] Move to an elif clause of the if in line 778, as it's just another combination of mode,self.mode under the master transparency condition --- PIL/Image.py | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/PIL/Image.py b/PIL/Image.py index ec25f1027..5d23a278a 100644 --- a/PIL/Image.py +++ b/PIL/Image.py @@ -802,7 +802,8 @@ class Image: # after quantization. trns_im = trns_im.convert('RGB') trns = trns_im.getpixel((0,0)) - + elif self.mode == 'P' and mode == 'RGBA' and 'transparency' in self.info: + delete_trns = True if mode == "P" and palette == ADAPTIVE: im = self.im.quantize(colors) @@ -837,9 +838,6 @@ class Image: except KeyError: raise ValueError("illegal conversion") - if self.mode == 'P' and mode == 'RGBA' and 'transparency' in self.info: - delete_trns = True - new_im = self._new(im) if delete_trns: #crash fail if we leave a bytes transparency in an rgb/l mode. From 88d74883bb507ca42a5f203e46b0ada8a47fc1cc Mon Sep 17 00:00:00 2001 From: wiredfool Date: Mon, 19 May 2014 14:12:43 -0700 Subject: [PATCH 5/5] remove redundant transparency check --- PIL/Image.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/PIL/Image.py b/PIL/Image.py index 5d23a278a..08b0dbe7d 100644 --- a/PIL/Image.py +++ b/PIL/Image.py @@ -802,7 +802,7 @@ class Image: # after quantization. trns_im = trns_im.convert('RGB') trns = trns_im.getpixel((0,0)) - elif self.mode == 'P' and mode == 'RGBA' and 'transparency' in self.info: + elif self.mode == 'P' and mode == 'RGBA': delete_trns = True if mode == "P" and palette == ADAPTIVE: