From d4784bffb2d1f398b1eb16b1fe09874b13f78cea Mon Sep 17 00:00:00 2001 From: homm Date: Thu, 24 Nov 2016 03:30:36 +0300 Subject: [PATCH] return copy of the image if size matches --- PIL/Image.py | 2 +- Tests/test_image_resample.py | 9 +++++++++ 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/PIL/Image.py b/PIL/Image.py index 368186f81..3f8a49e6f 100644 --- a/PIL/Image.py +++ b/PIL/Image.py @@ -1544,7 +1544,7 @@ class Image(object): size = tuple(size) if self.size == size: - return self._new(self.im) + return self.copy() if self.mode in ("1", "P"): resample = NEAREST diff --git a/Tests/test_image_resample.py b/Tests/test_image_resample.py index fb439579c..ebc1ac6e4 100644 --- a/Tests/test_image_resample.py +++ b/Tests/test_image_resample.py @@ -24,6 +24,15 @@ class TestImagingResampleVulnerability(PillowTestCase): with self.assertRaises(ValueError): im.resize((100, -100)) + def test_modify_after_resizing(self): + im = hopper('RGB') + # get copy with same size + copy = im.resize(im.size) + # some in-place operation + copy.paste('black', (0, 0, im.width // 2, im.height // 2)) + # image should be different + self.assertNotEqual(im.tobytes(), copy.tobytes()) + class TestImagingCoreResampleAccuracy(PillowTestCase): def make_case(self, mode, size, color):