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

Optimize getbbox() and getextrema() routines #8194

Merged
merged 6 commits into from
Sep 9, 2024

Conversation

homm
Copy link
Member

@homm homm commented Jul 2, 2024

Code for ipython:

from PIL import Image
im = Image.new('L', (2048, 2048), 0)
print('Worst case:', im.getbbox())
%timeit im.getbbox()
im = Image.open('tests/images/face1.png').resize((2048, 2048))
print('Normal case:', im.getbbox())
%timeit im.getbbox()
im = Image.open('tests/images/dices.png').resize((2048, 2048))
print('Normal case:', im.getbbox())
%timeit im.getbbox()
im = im.convert('RGB').convert('RGBA')
print('Best case:', im.getbbox())
%timeit im.getbbox()
im = im.convert('L')
print('Extrema:', im.getextrema())
%timeit im.getextrema()

main branch

Worst case: None
2.31 ms ± 3.29 μs per loop (mean ± std. dev. of 7 runs, 100 loops each)
Normal case: (150, 474, 1802, 2048)
3.97 ms ± 519 ns per loop (mean ± std. dev. of 7 runs, 100 loops each)
Normal case: (286, 70, 1724, 1797)
3.86 ms ± 461 ns per loop (mean ± std. dev. of 7 runs, 100 loops each)
Best case: (0, 0, 2048, 2048)
4.6 ms ± 464 ns per loop (mean ± std. dev. of 7 runs, 100 loops each)
Extrema: (0, 255)
2.3 ms ± 468 ns per loop (mean ± std. dev. of 7 runs, 100 loops each)

optimize-getbbox branch

Worst case: None
1.16 ms ± 1.65 μs per loop (mean ± std. dev. of 7 runs, 1,000 loops each)
Normal case: (150, 474, 1802, 2048)
722 μs ± 161 ns per loop (mean ± std. dev. of 7 runs, 1,000 loops each)
Normal case: (286, 70, 1724, 1797)
874 μs ± 260 ns per loop (mean ± std. dev. of 7 runs, 1,000 loops each)
Best case: (0, 0, 2048, 2048)
2.51 μs ± 18.1 ns per loop (mean ± std. dev. of 7 runs, 100,000 loops each)
Extrema: (0, 255)
97.6 μs ± 5.74 ns per loop (mean ± std. dev. of 7 runs, 10,000 loops each)
Worst case: 1.99x speedup
Normal case: 5.5x speedup
Normal case: 4.4x speedup
Best case: 1833x speedup
Extrema: 23.5x speedup

dices.png test image

dices

face1.png test image

face1

@radarhere
Copy link
Member

Could you provide a copy of face1.png?

@homm
Copy link
Member Author

homm commented Aug 14, 2024

@radarhere Ok, added, but it's just some random image from stocks)

@homm homm force-pushed the optimize-getbbox branch from 4c290ee to e4e2cd6 Compare August 14, 2024 11:29
@homm
Copy link
Member Author

homm commented Aug 14, 2024

Rebased

@radarhere radarhere merged commit 63f398b into python-pillow:main Sep 9, 2024
52 checks passed
@homm homm deleted the optimize-getbbox branch September 10, 2024 09:59
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.

4 participants