From ea0071309919cf1848fc46f9137165659049a701 Mon Sep 17 00:00:00 2001 From: Marco De Donno Date: Sun, 3 Jul 2016 21:38:36 +0200 Subject: [PATCH 1/8] Add a scale function to expand or contract a PIL image by a factor `factor`passed in argument. --- PIL/Image.py | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/PIL/Image.py b/PIL/Image.py index 64f461358..d0152b52b 100644 --- a/PIL/Image.py +++ b/PIL/Image.py @@ -1547,6 +1547,26 @@ class Image(object): return self._new(self.im.resize(size, resample)) + def scale(self, factor, resample = NEAREST): + """ + Returns a rescaled image by a specific factor given in parameter. + A factor greater than 1 expands the image, between 0 and 1 contracts the + image. + + :param factor: The expansion factor, as a float. + :param resample: An optional resampling filter. Same values possible as + in the PIL.Image.resize function. + :returns: An :py:class:`~PIL.Image.Image` object. + """ + if factor == 1: + return self._new(self.im) + elif factor <= 0: + raise ValueError("the factor must be greater than 0") + else: + size = (int(round(factor * self.width)), + int(round(factor * self.height))) + return self.resize(size, resample) + def rotate(self, angle, resample=NEAREST, expand=0): """ Returns a rotated copy of this image. This method returns a From c0eb87cac32a359222df8aa40e5ca33ac05d9a63 Mon Sep 17 00:00:00 2001 From: Marco De Donno Date: Tue, 5 Jul 2016 01:32:06 +0200 Subject: [PATCH 2/8] Move the scale function from Image to ImageOps --- PIL/Image.py | 20 -------------------- PIL/ImageOps.py | 21 +++++++++++++++++++++ 2 files changed, 21 insertions(+), 20 deletions(-) diff --git a/PIL/Image.py b/PIL/Image.py index d0152b52b..64f461358 100644 --- a/PIL/Image.py +++ b/PIL/Image.py @@ -1547,26 +1547,6 @@ class Image(object): return self._new(self.im.resize(size, resample)) - def scale(self, factor, resample = NEAREST): - """ - Returns a rescaled image by a specific factor given in parameter. - A factor greater than 1 expands the image, between 0 and 1 contracts the - image. - - :param factor: The expansion factor, as a float. - :param resample: An optional resampling filter. Same values possible as - in the PIL.Image.resize function. - :returns: An :py:class:`~PIL.Image.Image` object. - """ - if factor == 1: - return self._new(self.im) - elif factor <= 0: - raise ValueError("the factor must be greater than 0") - else: - size = (int(round(factor * self.width)), - int(round(factor * self.height))) - return self.resize(size, resample) - def rotate(self, angle, resample=NEAREST, expand=0): """ Returns a rotated copy of this image. This method returns a diff --git a/PIL/ImageOps.py b/PIL/ImageOps.py index f317645b2..a0fa9e41d 100644 --- a/PIL/ImageOps.py +++ b/PIL/ImageOps.py @@ -178,6 +178,27 @@ def crop(image, border=0): ) +def scale(self, factor, resample=Image.NEAREST): + """ + Returns a rescaled image by a specific factor given in parameter. + A factor greater than 1 expands the image, between 0 and 1 contracts the + image. + + :param factor: The expansion factor, as a float. + :param resample: An optional resampling filter. Same values possible as + in the PIL.Image.resize function. + :returns: An :py:class:`~PIL.Image.Image` object. + """ + if factor == 1: + return self._new(self.im) + elif factor <= 0: + raise ValueError("the factor must be greater than 0") + else: + size = (int(round(factor * self.width)), + int(round(factor * self.height))) + return self.resize(size, resample) + + def deform(image, deformer, resample=Image.BILINEAR): """ Deform the image. From 7af3c4c3bcde86e2bfe31b3d2859f96275810546 Mon Sep 17 00:00:00 2001 From: Marco De Donno Date: Tue, 5 Jul 2016 20:15:14 +0200 Subject: [PATCH 3/8] Add test for the ImageOps.scale function --- Tests/test_imageops.py | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/Tests/test_imageops.py b/Tests/test_imageops.py index 396f0da6c..47daef334 100644 --- a/Tests/test_imageops.py +++ b/Tests/test_imageops.py @@ -78,6 +78,16 @@ class TestImageOps(PillowTestCase): ImageOps.equalize(i.convert("P")) ImageOps.equalize(i.convert("RGB")) + def test_scale(self): + # Test the scaling function + i = hopper("L").resize((50,50)) + + newimg = ImageOps.scale(i,2) + self.assertEqual(newimg.size, (100, 100)) + + newimg = ImageOps.scale(i,0.5) + self.assertEqual(newimg.size, (25, 25)) + if __name__ == '__main__': unittest.main() From 4d51a410d8b237c9e284a6ccc62ad84226fd4c50 Mon Sep 17 00:00:00 2001 From: Marco De Donno Date: Tue, 5 Jul 2016 20:46:47 +0200 Subject: [PATCH 4/8] Add the test for factor = 1 and -1 --- Tests/test_imageops.py | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/Tests/test_imageops.py b/Tests/test_imageops.py index 47daef334..6873b6e11 100644 --- a/Tests/test_imageops.py +++ b/Tests/test_imageops.py @@ -81,7 +81,13 @@ class TestImageOps(PillowTestCase): def test_scale(self): # Test the scaling function i = hopper("L").resize((50,50)) - + + with self.assertRaises(ValueError): + ImageOps.scale(i,-1) + + newimg = ImageOps.scale(i,1) + self.assertEqual(newimg.size, (50, 50)) + newimg = ImageOps.scale(i,2) self.assertEqual(newimg.size, (100, 100)) From 7d8fea012be342fad0c74eb465583ce69f557eb7 Mon Sep 17 00:00:00 2001 From: Marco De Donno Date: Wed, 6 Jul 2016 01:32:16 +0200 Subject: [PATCH 5/8] Code style update --- PIL/ImageOps.py | 34 +++++++++++++++++----------------- Tests/test_imageops.py | 2 +- 2 files changed, 18 insertions(+), 18 deletions(-) diff --git a/PIL/ImageOps.py b/PIL/ImageOps.py index a0fa9e41d..2b3496e56 100644 --- a/PIL/ImageOps.py +++ b/PIL/ImageOps.py @@ -179,24 +179,24 @@ def crop(image, border=0): def scale(self, factor, resample=Image.NEAREST): - """ - Returns a rescaled image by a specific factor given in parameter. - A factor greater than 1 expands the image, between 0 and 1 contracts the - image. + """ + Returns a rescaled image by a specific factor given in parameter. + A factor greater than 1 expands the image, between 0 and 1 contracts the + image. - :param factor: The expansion factor, as a float. - :param resample: An optional resampling filter. Same values possible as - in the PIL.Image.resize function. - :returns: An :py:class:`~PIL.Image.Image` object. - """ - if factor == 1: - return self._new(self.im) - elif factor <= 0: - raise ValueError("the factor must be greater than 0") - else: - size = (int(round(factor * self.width)), - int(round(factor * self.height))) - return self.resize(size, resample) + :param factor: The expansion factor, as a float. + :param resample: An optional resampling filter. Same values possible as + in the PIL.Image.resize function. + :returns: An :py:class:`~PIL.Image.Image` object. + """ + if factor == 1: + return self._new(self.im) + elif factor <= 0: + raise ValueError("the factor must be greater than 0") + else: + size = (int(round(factor * self.width)), + int(round(factor * self.height))) + return self.resize(size, resample) def deform(image, deformer, resample=Image.BILINEAR): diff --git a/Tests/test_imageops.py b/Tests/test_imageops.py index 6873b6e11..fa763e09b 100644 --- a/Tests/test_imageops.py +++ b/Tests/test_imageops.py @@ -80,7 +80,7 @@ class TestImageOps(PillowTestCase): def test_scale(self): # Test the scaling function - i = hopper("L").resize((50,50)) + i = hopper("L").resize((50, 50)) with self.assertRaises(ValueError): ImageOps.scale(i,-1) From f19c52b5d5fd4b240e524e100b9358c88e7fd969 Mon Sep 17 00:00:00 2001 From: Marco De Donno Date: Wed, 6 Jul 2016 10:21:00 +0200 Subject: [PATCH 6/8] Code style update --- Tests/test_imageops.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/Tests/test_imageops.py b/Tests/test_imageops.py index fa763e09b..f06f284e6 100644 --- a/Tests/test_imageops.py +++ b/Tests/test_imageops.py @@ -83,15 +83,15 @@ class TestImageOps(PillowTestCase): i = hopper("L").resize((50, 50)) with self.assertRaises(ValueError): - ImageOps.scale(i,-1) + ImageOps.scale(i, -1) - newimg = ImageOps.scale(i,1) + newimg = ImageOps.scale(i, 1) self.assertEqual(newimg.size, (50, 50)) - newimg = ImageOps.scale(i,2) + newimg = ImageOps.scale(i, 2) self.assertEqual(newimg.size, (100, 100)) - newimg = ImageOps.scale(i,0.5) + newimg = ImageOps.scale(i, 0.5) self.assertEqual(newimg.size, (25, 25)) From 47ebf695acdd600f0026b181fd8fdfaced6ea41b Mon Sep 17 00:00:00 2001 From: Marco De Donno Date: Mon, 11 Jul 2016 12:00:44 +0200 Subject: [PATCH 7/8] Patch : Change from self to image --- PIL/ImageOps.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/PIL/ImageOps.py b/PIL/ImageOps.py index 2b3496e56..8bf6e7a44 100644 --- a/PIL/ImageOps.py +++ b/PIL/ImageOps.py @@ -178,7 +178,7 @@ def crop(image, border=0): ) -def scale(self, factor, resample=Image.NEAREST): +def scale(image, factor, resample=Image.NEAREST): """ Returns a rescaled image by a specific factor given in parameter. A factor greater than 1 expands the image, between 0 and 1 contracts the @@ -190,13 +190,13 @@ def scale(self, factor, resample=Image.NEAREST): :returns: An :py:class:`~PIL.Image.Image` object. """ if factor == 1: - return self._new(self.im) + return Image.Image._new(image.im) elif factor <= 0: raise ValueError("the factor must be greater than 0") else: - size = (int(round(factor * self.width)), - int(round(factor * self.height))) - return self.resize(size, resample) + size = (int(round(factor * image.width)), + int(round(factor * image.height))) + return image.resize(size, resample) def deform(image, deformer, resample=Image.BILINEAR): From 8355a34c1469e6b81eb7e29dde1376b96cfbd577 Mon Sep 17 00:00:00 2001 From: Marco De Donno Date: Mon, 11 Jul 2016 12:18:07 +0200 Subject: [PATCH 8/8] patch : image copy --- PIL/ImageOps.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/PIL/ImageOps.py b/PIL/ImageOps.py index 8bf6e7a44..8580ec5fb 100644 --- a/PIL/ImageOps.py +++ b/PIL/ImageOps.py @@ -190,7 +190,7 @@ def scale(image, factor, resample=Image.NEAREST): :returns: An :py:class:`~PIL.Image.Image` object. """ if factor == 1: - return Image.Image._new(image.im) + return image.copy() elif factor <= 0: raise ValueError("the factor must be greater than 0") else: