From 217e9628c73a148694641d11d1177d402d9e057c Mon Sep 17 00:00:00 2001 From: Karl Hobley Date: Mon, 27 Jul 2015 12:11:02 +0100 Subject: [PATCH] Fix to the way crop rects are rounded Eight and bottom were previously rounded down which sometimes led to images having a width/height of 0 This fixes a ZeroDivisionError and potential "tile cannot extend outside image" errors when resizing JPEGs --- wagtail/wagtailimages/image_operations.py | 3 +- .../tests/test_image_operations.py | 29 +++++++++++++++++-- 2 files changed, 29 insertions(+), 3 deletions(-) diff --git a/wagtail/wagtailimages/image_operations.py b/wagtail/wagtailimages/image_operations.py index a84bc805b..d1d90da7e 100644 --- a/wagtail/wagtailimages/image_operations.py +++ b/wagtail/wagtailimages/image_operations.py @@ -1,6 +1,7 @@ from __future__ import division import inspect +import math from wagtail.wagtailimages.exceptions import InvalidFilterSpecError @@ -158,7 +159,7 @@ class FillOperation(Operation): bottom = image_height # Crop! - willow.crop((int(left), int(top), int(right), int(bottom))) + willow.crop((math.floor(left), math.floor(top), math.ceil(right), math.ceil(bottom))) # Get scale for resizing # The scale should be the same for both the horizontal and diff --git a/wagtail/wagtailimages/tests/test_image_operations.py b/wagtail/wagtailimages/tests/test_image_operations.py index 9e6ddc6a0..8269ab553 100644 --- a/wagtail/wagtailimages/tests/test_image_operations.py +++ b/wagtail/wagtailimages/tests/test_image_operations.py @@ -154,7 +154,7 @@ class TestFillOperation(ImageOperationTestCase): # Basic usage with an oddly-sized original image # This checks for a rounding precision issue (#968) ('fill-200x200', Image(width=539, height=720), [ - ('crop', ((0, 90, 539, 629), ), {}), + ('crop', ((0, 90, 539, 630), ), {}), ('resize', ((200, 200), ), {}), ]), @@ -258,7 +258,32 @@ class TestFillOperation(ImageOperationTestCase): ), [ # This operation could probably be optimised out ('crop', ((0, 0, 1500, 1500), ), {}), - ]) + ]), + + + # A few tests for single pixel images + + ('fill-100x100', Image( + width=1, + height=1, + ), [ + ('crop', ((0, 0, 1, 1), ), {}), + ]), + + # This one once gave a ZeroDivisionError + ('fill-100x150', Image( + width=1, + height=1, + ), [ + ('crop', ((0, 0, 1, 1), ), {}), + ]), + + ('fill-150x100', Image( + width=1, + height=1, + ), [ + ('crop', ((0, 0, 1, 1), ), {}), + ]), ] TestFillOperation.setup_test_methods()