Skip to content

Add WebP decoder - #1122

Closed
benoit-pierre wants to merge 9 commits into
python-pillow:masterfrom
benoit-pierre:webp
Closed

benoit-pierre wants to merge 9 commits into
python-pillow:masterfrom
benoit-pierre:webp

Conversation

@benoit-pierre

Copy link
Copy Markdown
Contributor

Summary

Add a WebP decoder:

  • support incremental decoding
  • support Image.draft (arbitrary scaling, RGB/RGBA output)
  • support reading Exif/ICC profile data even if Mux API is not available

Heap profiling benchmark

Versions

  • Python 2.7.9
  • libwebp 0.4.2

Inputs

Name File size Image size/mode/compression
medium-lossless 217.00 KB 1432x888 RGB lossless
medium-lossy 44.00 KB 1432x888 RGB lossy
big-lossless 3.00 MB 2019x3029 RGB lossless
big-lossy 523.00 KB 2019x3029 RGB lossy
huge-lossless 25.00 MB 11158x2156 RGB lossless
huge-lossy 3.00 MB 11158x2156 RGB lossy

Options

  • full: decode to full size
  • thumb: use thumbnail((512, 512))
  • block: force block mode storage
  • onepass: disable incremental decoding

Results

Input Version Options Open + load times Result storage type/size Peak heap usage
medium-lossless master full 0.05s + 0.00s = 0.06s block 1432x888 4.00 MB 11.57 MB
medium-lossless master thumb 0.03s + 0.02s = 0.05s block 512x317 634.00 KB 11.57 MB
medium-lossless webp full 0.02s + 0.03s = 0.05s block 1432x888 4.00 MB 12.64 MB
medium-lossless webp full, onepass 0.01s + 0.02s = 0.03s block 1432x888 4.00 MB 12.57 MB
medium-lossless webp thumb, onepass 0.02s + 0.03s = 0.05s block 512x317 634.00 KB 8.36 MB
medium-lossy master full 0.02s + 0.00s = 0.03s block 1432x888 4.00 MB 11.16 MB
medium-lossy master thumb 0.03s + 0.03s = 0.06s block 512x317 634.00 KB 11.16 MB
medium-lossy webp full 0.01s + 0.01s = 0.02s block 1432x888 4.00 MB 7.65 MB
medium-lossy webp thumb 0.01s + 0.01s = 0.02s block 512x317 634.00 KB 3.43 MB
big-lossless master full 0.13s + 0.01s = 0.14s array 2019x3029 23.00 MB 51.24 MB
big-lossless master thumb 0.14s + 0.07s = 0.21s block 341x512 682.00 KB 51.24 MB
big-lossless webp full 0.01s + 0.14s = 0.16s array 2019x3029 23.00 MB 71.07 MB
big-lossless webp full, onepass 0.02s + 0.14s = 0.16s array 2019x3029 23.00 MB 71.01 MB
big-lossless webp full, onepass, block 0.01s + 0.11s = 0.12s block 2019x3029 23.00 MB 53.47 MB
big-lossless webp thumb, onepass 0.02s + 0.17s = 0.19s block 341x512 682.00 KB 30.80 MB
big-lossy master full 0.11s + 0.02s = 0.12s array 2019x3029 23.00 MB 43.55 MB
big-lossy master thumb 0.08s + 0.08s = 0.17s block 341x512 682.00 KB 43.55 MB
big-lossy webp full 0.01s + 0.08s = 0.09s array 2019x3029 23.00 MB 43.98 MB
big-lossy webp full, block 0.02s + 0.08s = 0.10s block 2019x3029 23.00 MB 26.44 MB
big-lossy webp thumb 0.02s + 0.08s = 0.09s block 341x512 682.00 KB 3.72 MB
huge-lossless master full 0.68s + 0.06s = 0.74s array 11158x2156 91.00 MB 217.80 MB
huge-lossless master thumb 0.68s + 0.24s = 0.92s block 512x98 196.00 KB 217.80 MB
huge-lossless webp full 0.01s + 1.66s = 1.68s array 11158x2156 91.00 MB 283.80 MB
huge-lossless webp full, onepass 0.01s + 0.69s = 0.71s array 11158x2156 91.00 MB 283.80 MB
huge-lossless webp full, onepass, block 0.02s + 0.65s = 0.66s block 11158x2156 91.00 MB 214.90 MB
huge-lossless webp thumb, onepass 0.01s + 0.71s = 0.72s block 512x98 196.00 KB 123.30 MB
huge-lossy master full 0.40s + 0.06s = 0.46s array 11158x2156 91.00 MB 163.40 MB
huge-lossy master thumb 0.40s + 0.24s = 0.64s block 512x98 196.00 KB 163.40 MB
huge-lossy webp full 0.01s + 0.42s = 0.43s array 11158x2156 91.00 MB 165.70 MB
huge-lossy webp full, block 0.01s + 0.36s = 0.38s block 11158x2156 91.00 MB 96.81 MB
huge-lossy webp thumb 0.02s + 0.34s = 0.36s block 512x98 196.00 KB 4.92 MB

Notes

  • Peak heap usage: as instrumented with valgrind --tool=massif.
  • Incremental decoding on lossless contents is really slow, and use more memory: decoding is only done at the end.
  • Normally, when an image storage size is bellow threshold (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 by block mode): twice the memory, no improvement with regards to fragmentation.

Remarks on the code

The final webp code:

  • Incremental decoding is automatically disabled on lossless contents (as with the benchmark onepass option).
  • Image storage mode is always forced to block (no matter the size, as with the benchmark block option).
  • The decoder output mode is always set to RGBA, even when asked to strip alpha: needed to be able to decode directly to the image storage, as the format for RGB is 4 bytes per-pixel. I did a few test with some transparent images, and it does not seem to cause any issue.

Regarding _webp.c:

  • It's still there for encoding support. I have not looked at the encoder side. I can if there is interest (and benefits in doing so): i.e. add a "proper" encoder. In which case, what would happen with WebPVersion, or WebPDecoderBuggyAlpha? What about backward compatibility with existing code that might check for WebP support by trying to import _webp?
  • Is WebPDecoderBuggyAlpha still needed? Version 0.1.3 is more than 4 years old (it was tagged in Git on September 2011).

@wiredfool

Copy link
Copy Markdown
Member

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: _webp.c -- I don't think that we have to worry about any code that's trying to import _webp, other than Pillow. It's a (by convention) private entity, and it hasn't been around long enough that people will have long buried workarounds digging into it like for imaging.

Re: WebPDecoderBuggyAlpha -- I think it's still needed since it's shipped in Ubuntu 12.04LTS. I don't want to break support for shipping libraries on currently supported LTS distros.

@benoit-pierre

Copy link
Copy Markdown
Contributor Author

The goal is to reduce memory usage (especially when generating thumbnails) with big images thanks to progressive decoding and draft support.

@homm

homm commented Mar 9, 2015

Copy link
Copy Markdown
Member

I see you throw on any errors including EXIF issues. It would be nice to support ImageFile.LOAD_TRUNCATED_IMAGES.

Comment thread PIL/WebPImagePlugin.py

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

return self maybe?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I looked at JpegImageFile.draft for reference, and the code is the same: no return self in this case.

@homm

homm commented Mar 9, 2015

Copy link
Copy Markdown
Member

In the draft method you assigning requested size to existing tile as is without modifications. Could you explain how is it possible? Who do interpolation? Which method is used? For example JPEGs can be downscaled in 2, 4, or 8 time relative to original size using supersampling interpolation.

@benoit-pierre

Copy link
Copy Markdown
Contributor Author

I see you throw on any errors including EXIF issues. It would be nice to support ImageFile.LOAD_TRUNCATED_IMAGES.

Will look into it.

In the draft method you assigning requested size to existing tile as is without modifications. Could you explain how is it possible? Who do interpolation? Which method is used?

Rescaling is done by the decoder on the fly. AFAIK the specific method used is not documented.

@benoit-pierre

Copy link
Copy Markdown
Contributor Author

@homm: I added support for ImageFile.LOAD_TRUNCATED_IMAGES.

@aclark4life

Copy link
Copy Markdown
Member

@benoit-pierre Thanks! @homm Are you happy with this?

Comment thread PIL/WebPImagePlugin.py

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What if self.im is already there and it is array storage?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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
When set, don't raise an error on truncated content.
@homm

homm commented Apr 1, 2015

Copy link
Copy Markdown
Member

I did some tests with draft method on this image.

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')

unsplash draft
unsplash load
(better to open in adjacent tabs and fast switch for comparing)

Draft version has some problems with colors. But I have only libwebp 0.4.0.

@benoit-pierre, could you show unsplash.draft.png for libwebp 0.4.2?

@homm

homm commented Apr 1, 2015

Copy link
Copy Markdown
Member

The quality of upscaling is poor. The same image as above upscaled to 800×530:

unsplash draft
unsplash load

We shouldn't use upscaling in .draft().

@benoit-pierre

Copy link
Copy Markdown
Contributor Author

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.

@aclark4life aclark4life modified the milestones: 2.9.0, 2.8.0 Apr 1, 2015
@homm

homm commented Apr 1, 2015

Copy link
Copy Markdown
Member

Faster compared to what? It is a little faster than BILINEAR, but much slower than NEAREST and looks like BILINEAR for x-pass and NEAREST for y-pass.

>>> %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 ms

We can't let user to decide, because in general user don't call WebPImageFile.draft(), he calls Image.draft() and don't know the actual format of image. We should try to make all methods more consistent and try to avoid special cases.

@benoit-pierre

Copy link
Copy Markdown
Contributor Author

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.

@benoit-pierre

Copy link
Copy Markdown
Contributor Author

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.

@benoit-pierre

Copy link
Copy Markdown
Contributor Author

Regarding the color issue, I'll look into it tomorrow.

@homm

homm commented Apr 1, 2015

Copy link
Copy Markdown
Member

draft() does not guarantee desired size and mode. This just a helper to instruct decoder how to do better. It shouldn't resize. Please, look at the code:

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 webp it will produce produce the same quality for all images except webp. This is what I mean by consistency.

@benoit-pierre

Copy link
Copy Markdown
Contributor Author
  • draft is only supported for JPEG, and a no-op for other image formats; in fact draft support was added for JPEG. See CHANGES.rst:
  • Added JPEG "save" and "draft" support for mode "YCbCr" images.
  • when using it you trade quality for speed, as mentioned in the JPEG section of docs/handbook/image-file-formats.rst:

Using the :py:meth:~PIL.Image.Image.draft method, you can speed things up by
converting RGB images to L, and resize images to 1/2, 1/4 or 1/8 of
their original size while loading them. The :py:meth:~PIL.Image.Image.draft
method also configures the JPEG decoder to trade some quality for speed.

  • draft currently never upscale because the JPEG decoder does not support it. I think it make sense to be able to do it for WebP if the decoder supports it.
  • I agree that consistency is important, so maybe Image.thumbnail should not use it per default, and an optional use_draft=False argument could be added?

@benoit-pierre

Copy link
Copy Markdown
Contributor Author

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.

@homm

homm commented Apr 2, 2015

Copy link
Copy Markdown
Member

So the documentation needs to be updated

Thanks for pointing this. #1170

when using it you trade quality for speed

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 ms

This 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.

@hugovk hugovk added the No Auto label Jun 19, 2015
@radarhere radarhere modified the milestones: 3.0.0, 2.9.0 Aug 19, 2015
@radarhere

Copy link
Copy Markdown
Member

Just to let you know, this PR now has merge conflicts.

@wiredfool

Copy link
Copy Markdown
Member

I can manage merge conflicts at review/merge time. (at least for these older, stuck in review PRs)

@alexwlchan

Copy link
Copy Markdown
Contributor

Resolves #2795.

@hugovk hugovk added the WebP label Oct 8, 2017
@wiredfool

Copy link
Copy Markdown
Member

I've got a working merge of this in my pr_1122 branch.

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.

7 participants