From 0a8206e7b62be4c72036caaf86b2284a4c27c9a5 Mon Sep 17 00:00:00 2001 From: Karl Hobley Date: Wed, 16 Jul 2014 13:36:51 +0100 Subject: [PATCH] Moved image processing code into wagtailimages/image_processor --- wagtail/wagtailimages/image_processor.py | 23 +++++++++++++++++++++++ wagtail/wagtailimages/models.py | 21 +++++---------------- 2 files changed, 28 insertions(+), 16 deletions(-) create mode 100644 wagtail/wagtailimages/image_processor.py diff --git a/wagtail/wagtailimages/image_processor.py b/wagtail/wagtailimages/image_processor.py new file mode 100644 index 000000000..c7ff21889 --- /dev/null +++ b/wagtail/wagtailimages/image_processor.py @@ -0,0 +1,23 @@ +from wagtail.wagtailimages.backends import get_image_backend +from wagtail.wagtailimages.utils import parse_filter_spec + + +def process_image(input_file, output_file, filter_spec, backend_name='default'): + # Get the image backend + backend = get_image_backend(backend_name) + + # Parse the filter spec + method_name, method_arg = parse_filter_spec(filter_spec) + + # Load image + image = backend.open_image(input_file) + file_format = image.format + + # Call method + method = getattr(backend, method_name) + image = method(image, method_arg) + + # Save image + backend.save_image(image, output_file, file_format) + + return output_file diff --git a/wagtail/wagtailimages/models.py b/wagtail/wagtailimages/models.py index 99d732efb..8145b90b1 100644 --- a/wagtail/wagtailimages/models.py +++ b/wagtail/wagtailimages/models.py @@ -20,7 +20,8 @@ from unidecode import unidecode from wagtail.wagtailadmin.taggable import TagSearchable from wagtail.wagtailimages.backends import get_image_backend from wagtail.wagtailsearch import indexed -from wagtail.wagtailimages.utils import validate_image_format, parse_filter_spec +from wagtail.wagtailimages.utils import validate_image_format +from wagtail.wagtailimages import image_processor @python_2_unicode_compatible @@ -149,21 +150,11 @@ class Filter(models.Model): generate an output image with this filter applied, returning it as another django.core.files.File object """ - backend = get_image_backend(backend_name) - - method_name, method_arg = parse_filter_spec(self.spec) - # If file is closed, open it input_file.open('rb') - image = backend.open_image(input_file) - file_format = image.format - # Call method - method = getattr(backend, method_name) - image = method(image, method_arg) - - output = BytesIO() - backend.save_image(image, output, file_format) + # Process the image + output = image_processor.process_image(input_file, BytesIO(), self.spec, backend_name=backend_name) # and then close the input file input_file.close() @@ -175,9 +166,7 @@ class Filter(models.Model): output_filename_parts = [filename_without_extension, self.spec] + input_filename_parts[-1:] output_filename = '.'.join(output_filename_parts) - output_file = File(output, name=output_filename) - - return output_file + return File(output, name=output_filename) class AbstractRendition(models.Model):