Repository navigation
Add WebP decoder - #1122
Add WebP decoder#1122benoit-pierre wants to merge 9 commits into
Conversation
|
Can you give a quick rundown on the motivation behind your changes? Re: Blockmode vs array mode -- That code has been in since way back in the PIL era. If I had to guess, it was more to ensure that you could alloc enough memory for large images by allowing it to be in chunks rather than require one massive block. It's reasonable to require one more or another for this use. Re: Re: |
|
The goal is to reduce memory usage (especially when generating thumbnails) with big images thanks to progressive decoding and draft support. |
|
I see you throw on any errors including EXIF issues. It would be nice to support |
There was a problem hiding this comment.
I looked at JpegImageFile.draft for reference, and the code is the same: no return self in this case.
|
In the |
Will look into it.
Rescaling is done by the decoder on the fly. AFAIK the specific method used is not documented. |
|
@homm: I added support for |
|
@benoit-pierre Thanks! @homm Are you happy with this? |
There was a problem hiding this comment.
What if self.im is already there and it is array storage?
There was a problem hiding this comment.
The code will still work: a temporary buffer will be allocated for decoding.
- support incremental decoding - support Image.draft - support reading Exif/ICC profile data even if Mux API is not available
It's apparently not a problem if we don't strip the alpha channel when decoding a RGBA image to RGB.
Two reasons: - it's *really* slow on lossless content - it's not incremental on lossless content: decoding will only be done at the end, after the whole data has been pushed to the decoder
This reduce memory usage.
When set, don't raise an error on truncated content.
|
I did some tests with This is my code: from PIL import Image
i = Image.open('unsplash.min.webp')
i.draft(None, (199, 132))
i.load()
i.save('unsplash.draft.png')
i = Image.open('unsplash.min.webp')
i.load()
i = i.resize((199, 132), Image.BICUBIC)
i.save('unsplash.load.png')
Draft version has some problems with colors. But I have only @benoit-pierre, could you show |
|
So? Let the user decide. Is it faster? I really don't see any downside in letting the user upscale with draft if he wants to. |
|
Faster compared to what? It is a little faster than >>> %time i = Image.open('unsplash.min.webp'); i.draft(None, (2048, 2048)); i.load()
Wall time: 116 ms
>>> %time i = Image.open('unsplash.min.webp'); i.load(); i = i.resize((2048, 2048), Image.BILINEAR)
Wall time: 143 ms
>>> %time i = Image.open('unsplash.min.webp'); i.load(); i = i.resize((2048, 2048), Image.NEAREST)
Wall time: 14.8 msWe can't let user to decide, because in general user don't call |
|
Draft is a special case. It only make sense for those decoders that support it, JPEG and WebP, and it's tied to the specific of each decoder. I you want consistency over different image formats, you should not use it, and prefer resize with the filter you want to use. |
|
Let me just add that with JPEG, since the decoder does not support arbitrary scaling, you will also get different results depending on the scaling factor: if you halve the resolution, than scaling will only be done by the decoder, while if you don't hit one of the supported factor, a second resize will be done by PIL. So the filter argument to Image.thumbnail may well be completely ignored. |
|
Regarding the color issue, I'll look into it tomorrow. |
|
i = Image.open('unknown image')
i.draft(None, (800, 530))
i.resize((800, 530), Image.BICUBIC)For now this code produce the same quality for all images. But with upscaling for |
|
|
Correction: apparently the JPEG decoder draft mode is never used as it was disabled by you (see 66cacc2). So the documentation needs to be updated, or draft re-enabled, and draft use in thumbnail made optional as mentioned in my previous comment. |
Thanks for pointing this. #1170
In case of JPEG downscaling we buy significant speedup for not so big quality lost (JPEG downscaling uses supersampling, which is not so bad). >>> %time i = Image.open('5k.jpg'); i.load(); i.resize((1280, 720), Image.BILINEAR)
Wall time: 427 ms
>>> %time i = Image.open('5k.jpg'); i.draft(None, (1280, 720)); i.load()
Wall time: 159 msThis is a good deal. For JPEG draft mode we have the noticeable quality lose with only 4.5% speedup (see #1029). Not so great. The same situation with WebP upscaling: we win about 19% in performance (for not-SIMD Pillow version and will lose for SIMD version, I want to note) but significantly lose in quality. WebP upscaling method even doesn't have a name, it just spoils the picture. |
|
Just to let you know, this PR now has merge conflicts. |
|
I can manage merge conflicts at review/merge time. (at least for these older, stuck in review PRs) |
|
Resolves #2795. |
|
I've got a working merge of this in my pr_1122 branch. |




Summary
Add a WebP decoder:
Image.draft(arbitrary scaling, RGB/RGBA output)Heap profiling benchmark
Versions
Inputs
Options
thumbnail((512, 512))Results
Notes
valgrind --tool=massif.(2048*2048*4 = 16M), array mode is used, instead of block mode. I assume this is done to reduce/handle memory fragmentation? The problem in this case is that the WebP decoder output buffer needs to be a contigous block of memory, and so we have to allocate a temporary buffer (of the same size as what would have been allocated byblock mode): twice the memory, no improvement with regards to fragmentation.Remarks on the code
The final
webpcode:block(no matter the size, as with the benchmark block option).Regarding
_webp.c:WebPVersion, orWebPDecoderBuggyAlpha? What about backward compatibility with existing code that might check for WebP support by trying to import_webp?WebPDecoderBuggyAlphastill needed? Version 0.1.3 is more than 4 years old (it was tagged in Git on September 2011).