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

Optimization encoding in 124 times #25

Open
wants to merge 9 commits into
base: master
Choose a base branch
from

Conversation

homm
Copy link

@homm homm commented Sep 24, 2024

Before:

In [1]: from blurhash import encode, Image
   ...: im = Image.open('/Code/imgs/bologna2k.jpg').copy()
   ...: %time im.size, encode(im.copy(), 9, 9)
CPU times: user 12.1 s, sys: 12.1 ms, total: 12.1 s
Wall time: 12.1 s
Out[1]: 
((2000, 1334),
 '|7E-z7oyM{8xM{wKTdWFNuwdMepHrE%LV[kVV@jFOVV@BBS$r@Naen={rrR7OrRQNaMKXmw|?aRPaLt6w0x[RQkqa1xuD*xZNuWEt6n%s-Vtf+M|x[ixxswctQnPNGR-RkXma0xtRkS~jF$%WXI;S~M|X8RjxYniR+ozs-')

After:

In [1]: from blurhash import encode, Image
   ...: im = Image.open('/Code/imgs/bologna2k.jpg').copy()
   ...: %time im.size, encode(im, 9, 9)
CPU times: user 141 ms, sys: 2.01 ms, total: 143 ms
Wall time: 143 ms
Out[1]: 
((2000, 1334),
 '|7E-z7oyM{8xM{wKTdWFNuwdMepHrE%LV[kVV@jFOVV@BBS$r@Naen={rrR7OrRQNaMKXmw|?aRPaLt6w0x[RQkqa1xuD*xZNuWEt6n%s-Vtf+M|x[ixxswctQnPNGR-RkXma0xtRkS~jF$%WXI;S~M|X8RjxYniR+ozs-')

Also improved:

Pillow image is only closed if it was opened inside the encoder.

Update

Improved more:

In [1]: from blurhash import encode, Image
   ...: im = Image.open('/Code/imgs/bologna2k.jpg').copy()
   ...: %time im.size, encode(im, 9, 9)
CPU times: user 70.4 ms, sys: 1.01 ms, total: 71.4 ms
Wall time: 71.4 ms
Out[1]: 
((2000, 1334),
 '|7E-z7oyM{8xM{wKTdWFNuwdMepHrE%LV[kVV@jFOVV@BBS$r@Naen={rrR7OrRQNaMKXmw|?aRPaLt6w0x[RQkqa1xuD*xZNuWEt6n%s-Vtf+M|x[ixxswctQnPNGR-RkXma0xtRkS~jF$%WXI;S~M|X8RjxYniR+ozs-')

@homm
Copy link
Author

homm commented Sep 24, 2024

For normal-size images speedup is also about 33x 53x 129x.

Before:

In [1]: from blurhash import encode, Image
   ...: im = Image.open('/Code/imgs/bologna2k.jpg').resize((360, 240), Image.BICUBIC)
   ...: assert encode(im.copy(), 6, 4) == 'W7EUfLs:M{8yM{wJ$%MepHrE%LV[NaVtBUXSsBNbVuOrM|NaMeXm'
   ...: %timeit encode(im.copy(), 6, 4)
125 ms ± 236 μs per loop (mean ± std. dev. of 7 runs, 10 loops each)

After:

In [1]: from blurhash import encode, Image
   ...: im = Image.open('/Code/imgs/bologna2k.jpg').resize((360, 240), Image.BICUBIC)
   ...: assert encode(im, 6, 4) == 'W7EUfLs:M{8yM{wJ$%MepHrE%LV[NaVtBUXSsBNbVuOrM|NaMeXm'
   ...: %timeit encode(im, 6, 4)
1.44 ms ± 3.13 μs per loop (mean ± std. dev. of 7 runs, 1,000 loops each)

Update

In [1]: from blurhash import encode, Image
   ...: im = Image.open('/Code/imgs/bologna2k.jpg').resize((360, 240), Image.BICUBIC)
   ...: assert encode(im, 6, 4) == 'W7EUfLs:M{8yM{wJ$%MepHrE%LV[NaVtBUXSsBNbVuOrM|NaMeXm'
   ...: %timeit encode(im, 6, 4)
971 μs ± 4.82 μs per loop (mean ± std. dev. of 7 runs, 1,000 loops each)

@homm homm changed the title Optimization encoding in 35x Optimization encoding in 53x Sep 24, 2024
@homm homm changed the title Optimization encoding in 53x Optimization encoding in 53 times Sep 24, 2024
@homm
Copy link
Author

homm commented Sep 25, 2024

Also back ported to upstream: woltapp/blurhash#256

@homm
Copy link
Author

homm commented Oct 11, 2024

I have updated branch to match the latest version from woltapp/blurhash#256

@lautat Are you interested in this improvements?

@homm homm changed the title Optimization encoding in 53 times Optimization encoding in 124 times Oct 11, 2024
@vellnes
Copy link

vellnes commented Dec 4, 2024

@lautat please note that
We will be very grateful for the optimization of the algorithm

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants