Moved smartcropping code from image backends into image model

pull/487/head
Karl Hobley 2014-07-21 11:14:53 +01:00
rodzic 9c3962aabc
commit c0175c749c
2 zmienionych plików z 21 dodań i 14 usunięć

Wyświetl plik

@ -1,6 +1,6 @@
from django.conf import settings
from wagtail.wagtailimages.utils import crop, focal_point, feature_detection
from wagtail.wagtailimages.utils import crop
class BaseImageBackend(object):
@ -56,18 +56,6 @@ class BaseImageBackend(object):
return image
def smart_crop(self, image, size):
image_mode, image_data = self.image_data_as_rgb(image)
# Use feature detection to find a focal point
feature_detector = feature_detection.FeatureDetector(image.size, image_mode, image_data)
focal_point = feature_detector.get_focal_point()
if focal_point:
return self.crop_to_point(image, size, focal_point)
# Fall back to crop to centre
return self.crop_to_centre(image, size)
def resize_to_max(self, image, size):
"""
Resize image down to fit within the given dimensions, preserving aspect ratio.

Wyświetl plik

@ -23,6 +23,7 @@ from wagtail.wagtailimages.backends import get_image_backend
from wagtail.wagtailsearch import indexed
from wagtail.wagtailimages.utils.validators import validate_image_format
from wagtail.wagtailimages.utils.focal_point import FocalPoint
from wagtail.wagtailimages.utils.feature_detection import FeatureDetector
@python_2_unicode_compatible
@ -69,7 +70,7 @@ class AbstractImage(models.Model, TagSearchable):
self.focal_point_x,
self.focal_point_y,
width=self.focal_point_width,
height=self.focal_point_height
height=self.focal_point_height,
)
@focal_point.setter
@ -79,6 +80,24 @@ class AbstractImage(models.Model, TagSearchable):
self.focal_point_width = focal_point.width
self.focal_point_height = focal_point.height
def get_suggested_focal_point(self, backend_name='default'):
backend = get_image_backend(backend_name)
image_file = self.file.file
# Make sure image is open and seeked to the beginning
image_file.open('rb')
image_file.seek(0)
# Load the image
image = backend.open_image(self.file.file)
image_mode, image_data = backend.image_data_as_rgb(image)
# Use feature detection to find a focal point
feature_detector = FeatureDetector(image.size, image_mode, image_data)
focal_point = feature_detector.get_focal_point()
return focal_point
def get_rendition(self, filter):
if not hasattr(filter, 'process_image'):
# assume we've been passed a filter spec string, rather than a Filter object