diff --git a/wagtail/wagtailimages/models.py b/wagtail/wagtailimages/models.py index cffc27377a..10b0727dd6 100644 --- a/wagtail/wagtailimages/models.py +++ b/wagtail/wagtailimages/models.py @@ -249,7 +249,7 @@ class AbstractImage(CollectionMember, TagSearchable): ) except Rendition.DoesNotExist: # Generate the rendition image - generated_image, output_format = filter.run(self, BytesIO()) + generated_image = filter.run(self, BytesIO()) # Generate filename input_filename = os.path.basename(self.file.name) @@ -262,7 +262,7 @@ class AbstractImage(CollectionMember, TagSearchable): 'gif': '.gif', } - output_extension = filter.spec.replace('|', '.') + FORMAT_EXTENSIONS[output_format] + output_extension = filter.spec.replace('|', '.') + FORMAT_EXTENSIONS[generated_image.format_name] if cache_key: output_extension = cache_key + '.' + output_extension @@ -273,7 +273,7 @@ class AbstractImage(CollectionMember, TagSearchable): rendition, created = self.renditions.get_or_create( filter=filter, focal_point_key=cache_key, - defaults={'file': File(generated_image, name=output_filename)} + defaults={'file': File(generated_image.f, name=output_filename)} ) return rendition @@ -387,8 +387,6 @@ class Filter(models.Model): for operation in self.operations: willow = operation.run(willow, image) or willow - output_format = original_format - if original_format == 'jpeg': # Allow changing of JPEG compression quality if hasattr(settings, 'WAGTAILIMAGES_JPEG_QUALITY'): @@ -396,22 +394,18 @@ class Filter(models.Model): else: quality = 85 - willow.save_as_jpeg(output, quality=quality) + return willow.save_as_jpeg(output, quality=quality) elif original_format == 'gif': # Convert image to PNG if it's not animated if not willow.has_animation(): - output_format = 'png' - willow.save_as_png(output) + return willow.save_as_png(output) else: - willow.save_as_gif(output) + return willow.save_as_gif(output) elif original_format == 'bmp': # Convert to PNG - output_format = 'png' - willow.save_as_png(output) + return willow.save_as_png(output) else: - willow.save(original_format, output) - - return output, output_format + return willow.save(original_format, output) def get_cache_key(self, image): vary_parts = [] diff --git a/wagtail/wagtailimages/views/images.py b/wagtail/wagtailimages/views/images.py index 5f2179c44c..21012963ab 100644 --- a/wagtail/wagtailimages/views/images.py +++ b/wagtail/wagtailimages/views/images.py @@ -211,8 +211,9 @@ def preview(request, image_id, filter_spec): image = get_object_or_404(get_image_model(), id=image_id) try: - response, image_format = Filter(spec=filter_spec).run(image, HttpResponse()) - response['Content-Type'] = 'image/' + image_format + response = HttpResponse() + image = Filter(spec=filter_spec).run(image, response) + response['Content-Type'] = 'image/' + image.format_name return response except InvalidFilterSpecError: return HttpResponse("Invalid filter spec: " + filter_spec, content_type='text/plain', status=400)