From 438c3cc0977de7b53decf0557d12fe302ea5d3a3 Mon Sep 17 00:00:00 2001 From: Andrew Murray Date: Fri, 30 Jul 2021 19:57:09 +1000 Subject: [PATCH] Speed up rotating square images by 90 or 270 degrees --- Tests/test_image_rotate.py | 3 +++ src/PIL/Image.py | 6 ++---- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/Tests/test_image_rotate.py b/Tests/test_image_rotate.py index 79ed79042..2d72ffa68 100644 --- a/Tests/test_image_rotate.py +++ b/Tests/test_image_rotate.py @@ -33,6 +33,9 @@ def test_angle(): with Image.open("Tests/images/test-card.png") as im: rotate(im, im.mode, angle) + im = hopper() + assert_image_equal(im.rotate(angle), im.rotate(angle, expand=1)) + def test_zero(): for angle in (0, 45, 90, 180, 270): diff --git a/src/PIL/Image.py b/src/PIL/Image.py index 124ca392b..f00f03c49 100644 --- a/src/PIL/Image.py +++ b/src/PIL/Image.py @@ -2074,10 +2074,8 @@ class Image: return self.copy() if angle == 180: return self.transpose(ROTATE_180) - if angle == 90 and expand: - return self.transpose(ROTATE_90) - if angle == 270 and expand: - return self.transpose(ROTATE_270) + if angle in (90, 270) and (expand or self.width == self.height): + return self.transpose(ROTATE_90 if angle == 90 else ROTATE_270) # Calculate the affine matrix. Note that this is the reverse # transformation (from destination image to source) because we