Using webp in R: A New Format for Lossless and Lossy Image Compression

January 24, 2016

(This article was first published on OpenCPU, and kindly contributed to R-bloggers)

opencpu logo

A while ago I blogged about the brotli, a new general purpose compression algorithm which Google promotes 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 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:

curl_download("", "pancake.jpg")
bitmap <- readJPEG("pancake.jpg")
write_webp(bitmap, "pancake.webp")

# Only works in Google Chrome

Of course it works the other way around as well. To read the webp image back into a bitmap and write it to png:

bitmap2 <- read_webp("pancake.webp")
writePNG(bitmap2, "pancake.png")

Rendering plots to webp

The easiest way to write plots in webp format is by using an svg device and render them to webp with the rsvg package (example from rsvg blog post):

# create an svg image
svglite("plot.svg", width = 10, height = 7)
qplot(mpg, wt, data = mtcars, colour = factor(cyl))

# render it into a bitmap array
rsvg_webp("plot.svg", "plot.webp", width = 1920)

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

tiger <- rsvg("", height = 720)
write_webp(tiger, "tiger100.webp", quality = 100)
write_webp(tiger, "tiger80.webp", quality = 80)
write_webp(tiger, "tiger50.webp", quality = 50)

Unfortunately webp will probably not become mainstream until it gets implemented by all browsers. However it could actually be valuable for scientific applications with large image compression.

To leave a comment for the author, please follow the link and comment on their blog: OpenCPU. offers daily e-mail updates about R news and tutorials on topics such as: Data science, Big Data, R jobs, visualization (ggplot2, Boxplots, maps, animation), programming (RStudio, Sweave, LaTeX, SQL, Eclipse, git, hadoop, Web Scraping) statistics (regression, PCA, time series, trading) and more...

If you got this far, why not subscribe for updates from the site? Choose your flavor: e-mail, twitter, RSS, or facebook...

Comments are closed.


Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)