From 39e407dbc112783393622ac8ebe06df89e69efa5 Mon Sep 17 00:00:00 2001 From: hugovk Date: Wed, 24 Feb 2016 15:06:28 +0200 Subject: [PATCH 1/2] Failing test for #1744 --- Tests/test_image_crop.py | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/Tests/test_image_crop.py b/Tests/test_image_crop.py index 29ff2bc2a..6edc71016 100644 --- a/Tests/test_image_crop.py +++ b/Tests/test_image_crop.py @@ -52,6 +52,20 @@ class TestImageCrop(PillowTestCase): self.assertEqual(len(im.getdata()), 0) self.assertRaises(IndexError, lambda: im.getdata()[0]) + def test_crop_float(self): + # Check cropping floats are rounded to nearest integer + # https://github.com/python-pillow/Pillow/issues/1744 + + # Arrange + im = Image.new("RGB", (10, 10)) + self.assertEqual(im.size, (10, 10)) + + # Act + cropped = im.crop((0.9, 1.1, 4.2, 5.8)) + + # Assert + self.assertEqual(cropped.size, (3, 5)) + if __name__ == '__main__': unittest.main() From 239f09435ae75eba733bfc5efb4eb505adcc5f13 Mon Sep 17 00:00:00 2001 From: hugovk Date: Wed, 24 Feb 2016 15:11:08 +0200 Subject: [PATCH 2/2] Round crop args to nearest integers (#1744) --- PIL/Image.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/PIL/Image.py b/PIL/Image.py index 6d0fe4fc3..964caf0d3 100644 --- a/PIL/Image.py +++ b/PIL/Image.py @@ -1941,7 +1941,9 @@ class _ImageCrop(Image): Image.__init__(self) - x0, y0, x1, y1 = box + # Round to nearest integer, runs int(round(x)) when unpacking + x0, y0, x1, y1 = map(int, map(round, box)) + if x1 < x0: x1 = x0 if y1 < y0: