mirror of
https://github.com/Hopiu/django-imagekit.git
synced 2026-03-16 21:30:23 +00:00
Extract Cover processor from Fill
This will also be used by `SmartFill`, so we might as well centralize it.
This commit is contained in:
parent
88cda1b0b3
commit
ce7353b0bd
1 changed files with 20 additions and 6 deletions
|
|
@ -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)
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue