Experimenting with PicToWebP

A blazingly fast, lightweight bulk image to WebP converter that can help you save over 90% of disk space

XQ
The Research Nest
4 min readSep 15, 2023

--

Photo by Héctor J. Rivas on Unsplash

A while ago, I wrote on LinkedIn about WebP image format. Here’s a quick summary.

  • Stop using JPEG and PNG images for online content creation.
  • Google introduced a new compression algorithm, creating the WebP image format.
  • Can reduce image sizes by > 60–80% compared to PNG.
  • Maintains high image quality despite the size reduction.
  • WebP format is supported by most browsers today.
  • Potential for PNG format to become obsolete due to WebP advantages.
  • Noted substantial reductions in image file size (for example, a Midjourney image from ~1.5 MB in PNG to ~100 KB in WebP).

Benefits of using WebP:

  • Faster page loading times.
  • Improved user retention.
  • Higher SEO scores.
  • Reduced storage space, saving costs.

WebP can also be beneficial in personal use cases:

  • Efficiently reduces storage needs in applications like Google Photos.
  • Avoids the necessity to upgrade storage subscription tiers by reducing overall file storage size.

Example demonstrating WebP efficiency:

  • PNG image size: 1.35 MB.
  • WebP version size: 57.58 KB.
  • Over 95% compression was achieved with no noticeable difference in quality.
The WebP version of this image was just 57.58kb with no noticeable difference in quality.

This basic exploration got me thinking. People were curious if some tools could help them convert their image libraries in bulk into WebP.

It is an interesting use case, and I decided to take it up as a mini-project of my own to create such a tool with a focus on performance. Fortunately, Pillow (PIL), a Python image processing library, has an inbuilt method to convert any image into the WebP format.

The basic idea is to create a utility wrapper around it to take in a bulk of images in a given source folder and efficiently convert them all into WebP format.

Hence, I created PicToWebP, a blazingly fast, lightweight bulk image-to-WebP converter that can help you save over 90% of disk space.

Let me not go too much into the technicals, but here are some results we see.

Yes. > 90% reduction in storage and not bluffing. Although I have yet to test different types of images, this holds true for AI-generated ones.

As an experiment, I generated 100 diverse images using SDXL 1.0. Each image was a 1024*1024px PNG file averaging around 1–1.5 MB in size.

I put them all into a folder called “imgs” (162 MB) and ran the PicToWebP script on it.

The resulting folder with the converted WebP images was just 11.4 MB. A whopping ~93% reduction in the storage memory used.

One hundred images were processed together blazingly fast, in 3.36 seconds. Thanks to the use of ThreadPoolExecutor from Concurrent.futures. Other features implemented into this tool, the cherries on the top of the cake, include — Reporting and logging to see the metrics of the memory saved and time taken.

The code is open-source, and I’m sure it can be extended well. Feel free to fork, clone, suggest changes, improve efficiency, add new features, and raise PRs. Customize at will. I am certain it can be made faster and better and improve compression.

Imagine if thousands of images are stored on the cloud like Google Drive or One Drive, and you are running out of space in your free tier. You can save enormous storage by running this script on your own images folder. The quality of WebP images is almost indistinguishable from that of PNG.

Almost every platform will support WebP as we advance. Most web browsers and applications already do.

Feel free to experiment and tinker with it. All you need is some basic knowledge of git and how to run a Python script to use it. For more info, check the GitHub repo.

Note: At the time of writing this article, Medium does not support WebP, but given the massive advantage it has, I am sure they will start supporting it soon. If you store your images in WebP and have to use them on some platform where it is not yet supported, you can think of converting that specific image back to PNG or JPEG or take a snippet screenshot of the WebP image on your laptop/mobile to make it in the required format.

--

--

XQ
The Research Nest

Exploring tech, life, and careers through content.