Using webp in R: A New Format for Lossless and Lossy Image Compression
January 25, 2016
A while ago I blogged about brotli, a new general purpose compression algorithm promoted by Google as an alternative to gzip. The same company also happens to be working on a new format for images called webp, which is actually a derivative of the VP8 video format. Google claims webp provides superior compression for both lossless (png) and lossy (jpeg) bitmaps, and even though the format is currently only supported in Google Chrome, it seems indeed promising.
The webp R package allows for reading/writing webp bitmap arrays so that we can convert between other bitmap formats. For example, let’s take this photo of a delicious and nutritious feelgoodbyfood spelt-pancake with coconut sprinkles and homemade espresso (see here for 7 other healthy winter breakfasts!)
We read the jpeg file into a bitmap and then write it to webp:
Of course it works the other way around as well. To read the webp image back into a bitmap and write it to png:
Rendering graphics to webp
The best way to write plots in webp format is using an svg device and then render to bitmap with the rsvg package:
write_webp function has a
quality parameter (integer between 1 and 100) which can be used to tune the quality-size trade-off for lossy compression. A
quality=100 equals lossless compression; the default
quality=80 provides considerable size reduction with negligible loss of quality.
Unfortunately webp will probably not become mainstream until it gets implemented by all browsers. But performance seems pretty good so perhaps it could actually be useful for large image compression in scientific applications.