Refactored crop_to_centre actions to use a utils method

pull/487/head
Karl Hobley 2014-07-17 12:54:09 +01:00
rodzic 6df6d7bf98
commit e3f8679862
3 zmienionych plików z 53 dodań i 31 usunięć

Wyświetl plik

@ -1,8 +1,10 @@
from __future__ import absolute_import
from .base import BaseImageBackend
import PIL.Image
from wagtail.wagtailimages.backends.base import BaseImageBackend
from wagtail.wagtailimages.utils.crop import crop_to_centre
class PillowBackend(BaseImageBackend):
def __init__(self, params):
@ -21,18 +23,9 @@ class PillowBackend(BaseImageBackend):
return image.resize(size, PIL.Image.ANTIALIAS)
def crop_to_centre(self, image, size):
(original_width, original_height) = image.size
(target_width, target_height) = size
crop_box = crop_to_centre(image.size, size)
# final dimensions should not exceed original dimensions
final_width = min(original_width, target_width)
final_height = min(original_height, target_height)
if final_width == original_width and final_height == original_height:
if crop_box.size != image.size:
return image.crop(crop_box)
else:
return image
left = (original_width - final_width) / 2
top = (original_height - final_height) / 2
return image.crop(
(left, top, left + final_width, top + final_height)
)

Wyświetl plik

@ -1,9 +1,11 @@
from __future__ import absolute_import
from .base import BaseImageBackend
from wand.image import Image
from wand.api import library
from wagtail.wagtailimages.backends.base import BaseImageBackend
from wagtail.wagtailimages.utils.crop import crop_to_centre
class WandBackend(BaseImageBackend):
def __init__(self, params):
@ -25,21 +27,13 @@ class WandBackend(BaseImageBackend):
return new_image
def crop_to_centre(self, image, size):
(original_width, original_height) = image.size
(target_width, target_height) = size
crop_box = crop_to_centre(image.size, size)
# final dimensions should not exceed original dimensions
final_width = min(original_width, target_width)
final_height = min(original_height, target_height)
if final_width == original_width and final_height == original_height:
if crop_box.size != image.size:
new_image = image.clone()
new_image.crop(
left=crop_box[0], top=crop_box[1], right=crop_box[2], bottom=crop_box[3]
)
return new_image
else:
return image
left = (original_width - final_width) / 2
top = (original_height - final_height) / 2
new_image = image.clone()
new_image.crop(
left=left, top=top, right=left + final_width, bottom=top + final_height
)
return new_image

Wyświetl plik

@ -0,0 +1,35 @@
class CropBox(object):
def __init__(self, left, top, right, bottom):
self.left = int(left)
self.top = int(top)
self.right = int(right)
self.bottom = int(bottom)
def __getitem__(self, key):
return (self.left, self.top, self.right, self.bottom)[key]
@property
def width(self):
return self.right - self.left
@property
def height(self):
return self.bottom - self.top
@property
def size(self):
return self.width, self.height
def crop_to_centre(image_size, crop_size):
(original_width, original_height) = image_size
(crop_width, crop_height) = crop_size
# final dimensions should not exceed original dimensions
final_width = min(original_width, crop_width)
final_height = min(original_height, crop_height)
left = (original_width - final_width) / 2
top = (original_height - final_height) / 2
return CropBox(left, top, left + final_width, top + final_height)