From 18014126937b72c77da782e33529357f6c83c028 Mon Sep 17 00:00:00 2001 From: Karl Hobley Date: Mon, 21 Jul 2014 16:29:01 +0100 Subject: [PATCH] Run smartcropping calculations on image save --- wagtail/wagtailimages/models.py | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/wagtail/wagtailimages/models.py b/wagtail/wagtailimages/models.py index f8d87de819..5e2941d3dd 100644 --- a/wagtail/wagtailimages/models.py +++ b/wagtail/wagtailimages/models.py @@ -8,7 +8,7 @@ from taggit.managers import TaggableManager from django.core.files import File from django.core.exceptions import ImproperlyConfigured, ObjectDoesNotExist from django.db import models -from django.db.models.signals import pre_delete +from django.db.models.signals import pre_delete, pre_save from django.dispatch.dispatcher import receiver from django.utils.safestring import mark_safe from django.utils.html import escape, format_html_join @@ -23,7 +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 +from wagtail.wagtailimages.utils.feature_detection import FeatureDetector, opencv_available @python_2_unicode_compatible @@ -180,6 +180,19 @@ class Image(AbstractImage): pass +# Do smartcropping calculations when user saves an image without a focal point +@receiver(pre_save, sender=Image) +def image_smartcrop(sender, instance, **kwargs): + if getattr(settings, 'WAGTAIL_SMARTCROP_ENABLED', False): + if not opencv_available: + raise ImproperlyConfigured("pyOpenCV could not be found.") + + # Make sure the image doesn't already have a focal point + if instance.focal_point is None: + # Set the focal point + instance.focal_point = instance.get_suggested_focal_point() + + # Receive the pre_delete signal and delete the file associated with the model instance. @receiver(pre_delete, sender=Image) def image_delete(sender, instance, **kwargs):