Removed a bunch of no longer used code

This commit is contained in:
Karl Hobley 2014-09-15 17:37:55 +01:00
parent c974975590
commit 7b4e8ca530
6 changed files with 44 additions and 214 deletions

View file

@ -2,7 +2,7 @@ from __future__ import division
from django.conf import settings
from wagtail.wagtailimages.utils import crop
from wagtail.wagtailimages.utils.rect import Rect
from wagtail.wagtailimages.utils.focal_point import FocalPoint
@ -38,27 +38,6 @@ class BaseImageBackend(object):
def crop(self, image, crop_box):
raise NotImplementedError('subclasses of BaseImageBackend must provide a crop() method')
def crop_to_centre(self, image, size):
crop_box = crop.crop_to_centre(image.size, size)
if crop_box.size != image.size:
return self.crop(image, crop_box)
else:
return image
def crop_to_point(self, image, size, focal_point):
crop_box = crop.crop_to_point(image.size, size, focal_point)
# Don't crop if we don't need to
if crop_box.size != image.size:
image = self.crop(image, crop_box)
# If the focal points are too large, the cropping system may not
# crop it fully, resize the image if this has happened:
if crop_box.size != size:
image = self.resize_to_fill(image, size)
return image
def resize_to_max(self, image, size, focal_point=None):
"""
Resize image down to fit within the given dimensions, preserving aspect ratio.
@ -224,7 +203,7 @@ class BaseImageBackend(object):
bottom = crop_y + crop_height / 2
# Crop!
return self.resize_to_min(self.crop(image, crop.CropBox(left, top, right, bottom)), size)
return self.resize_to_min(self.crop(image, Rect(left, top, right, bottom)), size)
def no_operation(self, image, param, focal_point=None):

View file

@ -21,8 +21,8 @@ class PillowBackend(BaseImageBackend):
image = image.convert('RGB')
return image.resize(size, PIL.Image.ANTIALIAS)
def crop(self, image, crop_box):
return image.crop(crop_box)
def crop(self, image, rect):
return image.crop(rect)
def image_data_as_rgb(self, image):
# https://github.com/thumbor/thumbor/blob/f52360dc96eedd9fc914fcf19eaf2358f7e2480c/thumbor/engines/pil.py#L206-L215

View file

@ -25,10 +25,10 @@ class WandBackend(BaseImageBackend):
new_image.resize(size[0], size[1])
return new_image
def crop(self, image, crop_box):
def crop(self, image, rect):
new_image = image.clone()
new_image.crop(
left=crop_box[0], top=crop_box[1], right=crop_box[2], bottom=crop_box[3]
left=rect[0], top=rect[1], right=rect[2], bottom=rect[3]
)
return new_image

View file

@ -25,8 +25,6 @@ from wagtail.wagtailimages.formats import (
from wagtail.wagtailimages.backends import get_image_backend
from wagtail.wagtailimages.backends.pillow import PillowBackend
from wagtail.wagtailimages.utils.crop import crop_to_point, CropBox
from wagtail.wagtailimages.utils.focal_point import FocalPoint
from wagtail.wagtailimages.utils.crypto import generate_signature, verify_signature
from wagtail.tests.models import EventPage, EventPageCarouselItem
from wagtail.wagtailcore.models import Page
@ -953,70 +951,6 @@ class TestGenerateURLView(TestCase, WagtailTestUtils):
}))
class TestCropToPoint(TestCase):
def test_basic(self):
"Test basic cropping in the centre of the image"
self.assertEqual(
crop_to_point((640, 480), (100, 100), FocalPoint(x=320, y=240)),
CropBox(270, 190, 370, 290),
)
def test_basic_no_focal_point(self):
"If focal point is None, it should make one in the centre of the image"
self.assertEqual(
crop_to_point((640, 480), (100, 100), None),
CropBox(270, 190, 370, 290),
)
def test_doesnt_exit_top_left(self):
"Test that the cropbox doesn't exit the image at the top left"
self.assertEqual(
crop_to_point((640, 480), (100, 100), FocalPoint(x=0, y=0)),
CropBox(0, 0, 100, 100),
)
def test_doesnt_exit_bottom_right(self):
"Test that the cropbox doesn't exit the image at the bottom right"
self.assertEqual(
crop_to_point((640, 480), (100, 100), FocalPoint(x=640, y=480)),
CropBox(540, 380, 640, 480),
)
def test_doesnt_get_smaller_than_focal_point(self):
"Test that the cropbox doesn't get any smaller than the focal point"
self.assertEqual(
crop_to_point((640, 480), (10, 10), FocalPoint(x=320, y=240, width=100, height=100)),
CropBox(270, 190, 370, 290),
)
def test_keeps_composition(self):
"Test that the cropbox tries to keep the composition of the original image as much as it can"
self.assertEqual(
crop_to_point((300, 300), (150, 150), FocalPoint(x=100, y=200)),
CropBox(50, 100, 200, 250), # Focal point is 1/3 across and 2/3 down in the crop box
)
def test_keeps_focal_point_in_view_bottom_left(self):
"""
Even though it tries to keep the composition of the image,
it shouldn't let that get in the way of keeping the entire subject in view
"""
self.assertEqual(
crop_to_point((300, 300), (150, 150), FocalPoint(x=100, y=200, width=150, height=150)),
CropBox(25, 125, 175, 275),
)
def test_keeps_focal_point_in_view_top_right(self):
"""
Even though it tries to keep the composition of the image,
it shouldn't let that get in the way of keeping the entire subject in view
"""
self.assertEqual(
crop_to_point((300, 300), (150, 150), FocalPoint(x=200, y=100, width=150, height=150)),
CropBox(125, 25, 275, 175),
)
class TestIssue573(TestCase):
"""
This tests for a bug which causes filename limit on Renditions to be reached

View file

@ -1,121 +0,0 @@
from __future__ import division
from wagtail.wagtailimages.utils.focal_point import FocalPoint
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 as_tuple(self):
return self.left, self.top, self.right, self.bottom
def __eq__(self, other):
return self.as_tuple() == other.as_tuple()
def __ne__(self, other):
return not (self == other)
def __repr__(self):
return 'CropBox(left: %d, top: %d, right: %d, bottom: %d)' % (
self.left, self.top, self.right, self.bottom
)
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)
def crop_to_point(image_size, crop_size, focal_point):
(original_width, original_height) = image_size
(crop_width, crop_height) = crop_size
if not focal_point:
focal_point = FocalPoint(original_width / 2, original_height / 2)
# Make sure that the crop size is no smaller than the focal point
crop_width = max(crop_width, focal_point.width)
crop_height = max(crop_height, focal_point.height)
# Make sure final dimensions do not exceed original dimensions
final_width = min(original_width, crop_width)
final_height = min(original_height, crop_height)
# Get UV for focal point
focal_point_u = focal_point.x / original_width
focal_point_v = focal_point.y / original_height
# Get crop box
left = focal_point.x - focal_point_u * final_width
top = focal_point.y - focal_point_v * final_height
right = focal_point.x - focal_point_u * final_width + final_width
bottom = focal_point.y - focal_point_v * final_height + final_height
# Make sure the entire focal point is in the crop box
focal_point_left = focal_point.x - focal_point.width / 2
focal_point_top = focal_point.y - focal_point.height / 2
focal_point_right = focal_point.x + focal_point.width / 2
focal_point_bottom = focal_point.y + focal_point.height / 2
if left > focal_point_left:
right -= left - focal_point_left
left = focal_point_left
if top > focal_point_top:
bottom -= top - focal_point_top
top = focal_point_top
if right < focal_point_right:
left += focal_point_right - right;
right = focal_point_right
if bottom < focal_point_bottom:
top += focal_point_bottom - bottom;
bottom = focal_point_bottom
# Don't allow the crop box to go over the image boundary
if left < 0:
right -= left
left = 0
if top < 0:
bottom -= top
top = 0
if right > original_width:
left -= right - original_width
right = original_width
if bottom > original_height:
top -= bottom - original_height
bottom = original_height
return CropBox(left, top, right, bottom)

View file

@ -0,0 +1,38 @@
from __future__ import division
class Rect(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 as_tuple(self):
return self.left, self.top, self.right, self.bottom
def __eq__(self, other):
return self.as_tuple() == other.as_tuple()
def __ne__(self, other):
return not (self == other)
def __repr__(self):
return 'Rect(left: %d, top: %d, right: %d, bottom: %d)' % (
self.left, self.top, self.right, self.bottom
)