Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Speed up rotating square images by 90 or 270 degrees #5646

Merged
merged 3 commits into from Aug 2, 2021

Conversation

radarhere
Copy link
Member

rotate() may use transpose() for a faster result. For 90 or 270 degrees, it makes sure that the expand is true, since rotating an image sideways may change the size... except if the image is a square, so I've created this PR.

The test does not fail without this change, since this is just a speed improvement.

Pillow/src/PIL/Image.py

Lines 2070 to 2080 in 6cb127c

# Fast paths regardless of filter, as long as we're not
# translating or changing the center.
if not (center or translate):
if angle == 0:
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)

@radarhere radarhere changed the title Ignoring expand flag when rotating square images by 90 or 270 degrees Ignore expand flag when rotating square images by 90 or 270 degrees Jul 30, 2021
@radarhere radarhere changed the title Ignore expand flag when rotating square images by 90 or 270 degrees Speed up rotating square images by 90 or 270 degrees Jul 30, 2021
@hugovk
Copy link
Member

hugovk commented Aug 2, 2021

What sort of speed increase does this give?

@radarhere
Copy link
Member Author

import timeit
setup = '''
from PIL import Image
im = Image.open("Tests/images/hopper.png")
'''
print(timeit.Timer('im.rotate(90)', setup=setup).repeat(number=10000))

# Without change: [0.23164604200000002, 0.23040333300000004, 0.22858733399999998, 0.22947383300000002, 0.228850459]
# With change: [0.10277529199999999, 0.10330620800000001, 0.10458945799999997, 0.10447408300000005, 0.10545916600000005]

So it makes it twice as fast.

@homm
Copy link
Member

homm commented Aug 2, 2021

It will be much more significant for larger images.

@hugovk
Copy link
Member

hugovk commented Aug 2, 2021

Thanks, this is worth including in the release notes as well.

@hugovk hugovk merged commit 5a9dacf into python-pillow:master Aug 2, 2021
@radarhere radarhere deleted the expand branch August 2, 2021 22:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants