From 5a63a0b74a5ea5344b9df1ffa39d81a8cf9916c0 Mon Sep 17 00:00:00 2001 From: Matt Westcott Date: Wed, 9 Sep 2015 17:15:38 +0100 Subject: [PATCH] Fix rounding errors when resizing 1x1 images (backport 217e9628c73a148694641d11d1177d402d9e057c to 0.8.x) --- wagtail/wagtailimages/backends/base.py | 6 +++++- wagtail/wagtailimages/tests/test_models.py | 22 ++++++++++++++++++++++ wagtail/wagtailimages/tests/utils.py | 4 ++-- 3 files changed, 29 insertions(+), 3 deletions(-) diff --git a/wagtail/wagtailimages/backends/base.py b/wagtail/wagtailimages/backends/base.py index afaf550703..865b405f74 100644 --- a/wagtail/wagtailimages/backends/base.py +++ b/wagtail/wagtailimages/backends/base.py @@ -1,5 +1,7 @@ from __future__ import division +import math + from django.conf import settings from wagtail.wagtailimages.rect import Rect @@ -242,7 +244,9 @@ class BaseImageBackend(object): bottom = im_height # Crop! - return self.resize_to_min(self.crop(image, Rect(left, top, right, bottom)), size) + return self.resize_to_min(self.crop(image, + Rect(math.floor(left), math.floor(top), math.ceil(right), math.ceil(bottom)) + ), size) def no_operation(self, image, param, focal_point=None): """Return the image unchanged""" diff --git a/wagtail/wagtailimages/tests/test_models.py b/wagtail/wagtailimages/tests/test_models.py index b867117c68..19c7975364 100644 --- a/wagtail/wagtailimages/tests/test_models.py +++ b/wagtail/wagtailimages/tests/test_models.py @@ -164,6 +164,28 @@ class TestRenditions(TestCase): self.assertEqual(first_rendition, second_rendition) +class TestSinglePixelRenditions(TestCase): + def setUp(self): + # Create an image for running tests on + self.image = Image.objects.create( + title="Test image", + file=get_test_image_file(width=1, height=1), + ) + + def test_resize(self): + rendition = self.image.get_rendition('fill-100x100') + self.assertEqual(rendition.width, 1) + self.assertEqual(rendition.height, 1) + + rendition = self.image.get_rendition('fill-100x150') + self.assertEqual(rendition.width, 1) + self.assertEqual(rendition.height, 1) + + rendition = self.image.get_rendition('fill-150x100') + self.assertEqual(rendition.width, 1) + self.assertEqual(rendition.height, 1) + + class TestRenditionsWand(TestCase): def setUp(self): try: diff --git a/wagtail/wagtailimages/tests/utils.py b/wagtail/wagtailimages/tests/utils.py index 62b88126f8..e5eb3db311 100644 --- a/wagtail/wagtailimages/tests/utils.py +++ b/wagtail/wagtailimages/tests/utils.py @@ -9,8 +9,8 @@ from wagtail.wagtailimages.models import get_image_model Image = get_image_model() -def get_test_image_file(filename='test.png'): +def get_test_image_file(filename='test.png', width=640, height=480): f = BytesIO() - image = PIL.Image.new('RGB', (640, 480), 'white') + image = PIL.Image.new('RGB', (width, height), 'white') image.save(f, 'PNG') return ImageFile(f, name=filename)