Skip to main content

How to bulk convert images to webp

So webp images is the new thing in the content creation world, because they render fast and they are not heavy, making your websites load up fast.

And as you know images is one of the assets that drags down your webpage loading speeds.

So knowing we need to adopt webp images, how do we deal with those many images that we've uploaded or we will be uploading to our next blog posts?

That brings us to Google WebP images Project, this tool helps us compress any image files to WebP image.

So you need to install the Webp Tool NOTE: The tool comes along with both

  • cwebp -- WebP encoder tool
  • dwebp -- WebP decoder tool
  • vwebp -- WebP file viewer
  • webpmux -- WebP muxing tool
  • gif2webp -- Tool for converting GIF images to WebP

So after installing Webp Tool for your specified OS, now it's time to write a bash script to automate the conversion process.

  1. Create bash file
touch webpbulk.sh
  1. Open the file you just created
nano webpbulk.sh
  1. Then paste the below script there, where you will be replacing the "/Users/yourusername/" with your Directory paths, like, /Users/saintmalik/, just the way it is on your system.
for file in /Users/yourusername/$1/*;
do cwebp -q 80 "$file" -o "${file%.*}.webp";
done
  1. Save the file and get all your images ready in your images folders

  2. Now run command below and it will bulk convert the image files to Webp format

bash webpbulk.sh /FOLDER-WHERE YOUR IMAGES ARE.

e.g

bash webpbulk.sh DevProjects/blog.saintmalik.me/static/bgimg
info

So at the end of the day, the directory path will look like this

/Users/abdulmalik//DevProjects/blog.saintmalik.me/static/bgimg/IMAGES-GETTING-COMPRESSED

Also you can read the documentation to see some tweaks you can add to it, or which flag options can be useful to you cwebp Encoder Docs.

Thats all.


Comments