Extract Cover processor from Fill

This will also be used by `SmartFill`, so we might as well centralize it.
This commit is contained in:
Matthew Tretter 2012-02-11 15:01:59 -05:00
parent 88cda1b0b3
commit ce7353b0bd

View file

@ -7,6 +7,25 @@ class Resize(object):
pass
class Cover(object):
"""
Resizes the image to the smallest possible size that will entirely cover the
provided dimensions. You probably won't be using this processor directly,
but it's used internally by ``Fill``.
"""
def __init__(self, width, height):
self.width, self.height = width, height
def process(self, img):
original_width, original_height = img.size
ratio = max(float(self.width) / original_width,
float(self.height) / original_height)
new_width, new_height = (int(original_width * ratio),
int(original_height * ratio))
return img.resize((new_width, new_height), Image.ANTIALIAS)
class Fill(object):
"""
Resizes an image , cropping it to the exact specified width and height.
@ -45,12 +64,7 @@ class Fill(object):
self.anchor = anchor
def process(self, img):
original_width, original_height = img.size
ratio = max(float(self.width) / original_width,
float(self.height) / original_height)
new_width, new_height = (int(original_width * ratio),
int(original_height * ratio))
img = img.resize((new_width, new_height), Image.ANTIALIAS)
img = Cover(self.width, self.height).process(img)
return crop.Crop(self.width, self.height,
anchor=self.anchor).process(img)