-
-
Notifications
You must be signed in to change notification settings - Fork 2.2k
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
Saving a PNG file is slow #1211
Comments
I just tested this code:
Running it:
Profiling it shows the call to
|
Running that same test on my machine, I get the same behavior. However, using an image of the original size and not being resized, the time taken increases. I should also add that in addition to the dimensions 4928x3264, the image is 20MB in size. Could multi-threading the encoding process improve this time?
|
@hugovk PNG compression time is very sensitive to input data. If you are compressing very small image (hopper.png) image resized with nearest filter (the default), it will be much faster than compressing arbitrary image without repeating colors. In [1]: from PIL import Image
In [2]: im = Image.open('Tests/images/hopper.png').resize((4928, 3264))
In [3]: %time im.save('png.png')
CPU times: user 544 ms, sys: 8 ms, total: 552 ms
Wall time: 556 ms
In [4]: im = Image.open('Tests/images/hopper.png').resize((4928, 3264), Image.BICUBIC)
In [5]: %time im.save('png.png')
CPU times: user 3.39 s, sys: 92 ms, total: 3.48 s
Wall time: 3.49 s @bouncehead13 PNG uses deflate algorithm for compression, which is asymmetrical (much slower compression than decompression). All you can do is reduce compression level (you can choose from 1 to 9, where 1 is lowest): In [5]: %time im.save('png.png')
CPU times: user 3.39 s, sys: 92 ms, total: 3.48 s
Wall time: 3.49 s
In [6]: %time im.save('png.png', compress_level=1)
CPU times: user 1.3 s, sys: 132 ms, total: 1.43 s
Wall time: 1.43 s |
@homm I did not know |
From zlib docs:
Pillow doesn't change this level if |
@homm Ah, of course, the resized image has lots of repeated colours. This creates an image with random black and white pixels:
Running it creates a 15M image (compared to 120K before):
|
@hugovk black and while is still only two colors and far from arbitrary image color distribution :) |
For the work I'm doing |
Hello,
I wan to create a PNG file from the data with dimensions 4928x3264. It takes roughly 9 seconds to save this data to a file. I am looking into improving this time for my work. Can you give a brief explanation to why this function takes a long time to complete? Also, is the saving process multi-threaded or does it happen using a single process.
Thanks!
Matt
The text was updated successfully, but these errors were encountered: