From f6d11a2803aa0c04a9470e94e658e28a767c0426 Mon Sep 17 00:00:00 2001 From: Andrew Murray Date: Thu, 6 Aug 2015 00:23:14 +1000 Subject: [PATCH] Only use fast rotate operations if the expand flag is in use or the image is square --- PIL/Image.py | 2 +- Tests/test_image_rotate.py | 14 +++++++++----- _imaging.c | 6 ++++-- 3 files changed, 14 insertions(+), 8 deletions(-) diff --git a/PIL/Image.py b/PIL/Image.py index 0c254c61d..77408e0fb 100644 --- a/PIL/Image.py +++ b/PIL/Image.py @@ -1621,7 +1621,7 @@ class Image(object): if self.mode in ("1", "P"): resample = NEAREST - return self._new(self.im.rotate(angle, resample)) + return self._new(self.im.rotate(angle, resample, expand)) def save(self, fp, format=None, **params): """ diff --git a/Tests/test_image_rotate.py b/Tests/test_image_rotate.py index 38391adae..26c0bd729 100644 --- a/Tests/test_image_rotate.py +++ b/Tests/test_image_rotate.py @@ -1,19 +1,23 @@ from helper import unittest, PillowTestCase, hopper +from PIL import Image class TestImageRotate(PillowTestCase): def test_rotate(self): - def rotate(mode): - im = hopper(mode) - out = im.rotate(45) + def rotate(im, mode, angle): + out = im.rotate(angle) self.assertEqual(out.mode, mode) self.assertEqual(out.size, im.size) # default rotate clips output - out = im.rotate(45, expand=1) + out = im.rotate(angle, expand=1) self.assertEqual(out.mode, mode) self.assertNotEqual(out.size, im.size) for mode in "1", "P", "L", "RGB", "I", "F": - rotate(mode) + im = hopper(mode) + rotate(im, mode, 45) + for angle in 90, 270: + im = Image.open('Tests/images/test-card.png') + rotate(im, im.mode, angle) if __name__ == '__main__': diff --git a/_imaging.c b/_imaging.c index 895faac54..7fd2da88e 100644 --- a/_imaging.c +++ b/_imaging.c @@ -1550,7 +1550,8 @@ _rotate(ImagingObject* self, PyObject* args) double theta; int filter = IMAGING_TRANSFORM_NEAREST; - if (!PyArg_ParseTuple(args, "d|i", &theta, &filter)) + int expand; + if (!PyArg_ParseTuple(args, "d|i|i", &theta, &filter, &expand)) return NULL; imIn = self->image; @@ -1563,7 +1564,8 @@ _rotate(ImagingObject* self, PyObject* args) /* Rotate with resampling filter */ imOut = ImagingNew(imIn->mode, imIn->xsize, imIn->ysize); (void) ImagingRotate(imOut, imIn, theta, filter); - } else if (theta == 90.0 || theta == 270.0) { + } else if ((theta == 90.0 || theta == 270.0) + && (expand || imIn->xsize == imIn->ysize)) { /* Use fast version */ imOut = ImagingNew(imIn->mode, imIn->ysize, imIn->xsize); if (imOut) {