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/6] 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 64f4613585b..d0152b52bf2 100644 --- a/PIL/Image.py +++ b/PIL/Image.py @@ -1547,6 +1547,26 @@ def resize(self, size, resample=NEAREST): 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/6] 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 d0152b52bf2..64f4613585b 100644 --- a/PIL/Image.py +++ b/PIL/Image.py @@ -1547,26 +1547,6 @@ def resize(self, size, resample=NEAREST): 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 f317645b259..a0fa9e41dcc 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/6] 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 396f0da6cbd..47daef3348b 100644 --- a/Tests/test_imageops.py +++ b/Tests/test_imageops.py @@ -78,6 +78,16 @@ def test_pil163(self): 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/6] 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 47daef3348b..6873b6e11e0 100644 --- a/Tests/test_imageops.py +++ b/Tests/test_imageops.py @@ -81,7 +81,13 @@ def test_pil163(self): 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/6] Code style update --- PIL/ImageOps.py | 36 ++++++++++++++++++------------------ Tests/test_imageops.py | 2 +- 2 files changed, 19 insertions(+), 19 deletions(-) diff --git a/PIL/ImageOps.py b/PIL/ImageOps.py index a0fa9e41dcc..2b3496e56b2 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. - - :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) + """ + 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): diff --git a/Tests/test_imageops.py b/Tests/test_imageops.py index 6873b6e11e0..fa763e09bb0 100644 --- a/Tests/test_imageops.py +++ b/Tests/test_imageops.py @@ -80,7 +80,7 @@ def test_pil163(self): 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/6] 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 fa763e09bb0..f06f284e65a 100644 --- a/Tests/test_imageops.py +++ b/Tests/test_imageops.py @@ -83,15 +83,15 @@ def test_scale(self): 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))