Skip to content

Commit

Permalink
Merge f19c52b into 9abdde1
Browse files Browse the repository at this point in the history
  • Loading branch information
mdedonno1337 committed Jul 6, 2016
2 parents 9abdde1 + f19c52b commit e1c8cef
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 0 deletions.
21 changes: 21 additions & 0 deletions PIL/ImageOps.py
Expand Up @@ -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.
Expand Down
16 changes: 16 additions & 0 deletions Tests/test_imageops.py
Expand Up @@ -78,6 +78,22 @@ 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))

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))

newimg = ImageOps.scale(i, 0.5)
self.assertEqual(newimg.size, (25, 25))


if __name__ == '__main__':
unittest.main()
Expand Down

0 comments on commit e1c8cef

Please sign in to comment.