Add a scale function to expand or contract a PIL image by a factor factorpassed in argument.

This commit is contained in:
Marco De Donno 2016-07-03 21:38:36 +02:00
parent 2023c7cea1
commit ea00713099

View File

@ -1547,6 +1547,26 @@ class Image(object):
return self._new(self.im.resize(size, resample)) 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): def rotate(self, angle, resample=NEAREST, expand=0):
""" """
Returns a rotated copy of this image. This method returns a Returns a rotated copy of this image. This method returns a