From e422f92e523f4d0d66611b2f9f54f6606a218d4b Mon Sep 17 00:00:00 2001 From: wiredfool Date: Thu, 31 Aug 2017 06:18:59 -0700 Subject: [PATCH] Remove palettes from non-palette modes in _new #2702 --- PIL/Image.py | 11 ++++++----- Tests/test_image.py | 27 +++++++++++++++++++++++++++ Tests/test_image_convert.py | 3 +++ 3 files changed, 36 insertions(+), 5 deletions(-) diff --git a/PIL/Image.py b/PIL/Image.py index 7e51cc832..94f282bcb 100644 --- a/PIL/Image.py +++ b/PIL/Image.py @@ -527,11 +527,12 @@ class Image(object): new.im = im new.mode = im.mode new.size = im.size - if self.palette: - new.palette = self.palette.copy() - if im.mode == "P" and not new.palette: - from . import ImagePalette - new.palette = ImagePalette.ImagePalette() + if im.mode in ('P', 'PA'): + if self.palette: + new.palette = self.palette.copy() + else: + from . import ImagePalette + new.palette = ImagePalette.ImagePalette() new.info = self.info.copy() return new diff --git a/Tests/test_image.py b/Tests/test_image.py index c6eaa7906..feac23a0b 100644 --- a/Tests/test_image.py +++ b/Tests/test_image.py @@ -446,6 +446,33 @@ class TestImage(PillowTestCase): self.assert_image_equal(im, target) + def test__new(self): + from PIL import ImagePalette + + im = hopper('RGB') + im_p = hopper('P') + + blank_p = Image.new('P', (10,10)) + blank_pa = Image.new('PA', (10,10)) + blank_p.palette = None + blank_pa.palette = None + + def _make_new(base_image, im, palette_result=None): + new_im = base_image._new(im) + self.assertEqual(new_im.mode, im.mode) + self.assertEqual(new_im.size, im.size) + self.assertEqual(new_im.info, base_image.info) + if palette_result is not None: + self.assertEqual(new_im.palette.tobytes(), palette_result.tobytes()) + else: + self.assertEqual(new_im.palette, None) + + _make_new(im, im_p, im_p.palette) + _make_new(im_p, im, None) + _make_new(im, blank_p, ImagePalette.ImagePalette()) + _make_new(im, blank_pa, ImagePalette.ImagePalette()) + + class MockEncoder(object): pass diff --git a/Tests/test_image_convert.py b/Tests/test_image_convert.py index f56e2f4ed..15cd03c47 100644 --- a/Tests/test_image_convert.py +++ b/Tests/test_image_convert.py @@ -87,6 +87,9 @@ class TestImageConvert(PillowTestCase): # Assert self.assertNotIn('transparency', rgba.info) + # https://github.com/python-pillow/Pillow/issues/2702 + self.assertEqual(rgba.palette, None) + def test_trns_l(self): im = hopper('L')