(UPDATED TO CORRECT FORMATTING)
We all know and use JPG images almost in a daily basis and the quality/filesize is remarkably good. However, there are newer file formats that are even better at retaining perceived image quality with smaller file sizes; one of those image formats is called WebP. This image format has been around for more than a decade already and is supported by most image viewers and web browsers today.
You can use WebP to store images in your computer and you can also use it for your web page to make them load faster since file sizes can be almost 50% less than a JPG with the same quality… By the way, WebP has a huge advantage over JPG, WebP supports alpha channel (transparency) and the resulting image can my much, much smaller than a PNG image (depending on the content).
So… let's say you have a folder with 50 photos of your last trip to the Bahamas, but each image is 10MB, so that entire folder is 500MB! Or maybe you're creating a webpage or blog and all the images and graphics make the page a bit too slow to load…
There are dozens of programs to convert images from one format to another, but some of those programs are paid, others are free but with limited functionality and others are not even available for Linux… As a graphic designer using Linux for more than 10 years, I've learned that the best tool to convert images/video/sound is FFMPEG through the Linux Terminal. Most linux distros (options) today come with FFMPEG installed, so in theory, you just need to open the Terminal and type the commands.
So let's begin!… Let's say the images you want to convert are inside a folder called "Pictures" which is located in
"/home/user/Pictures/"
… The command to convert all the images inside that folder will be:input_folder="/home/user/Pictures/" ; cd "$input_folder" && mkdir -p webp && cp -u "$input_folder"/* "$input_folder"/webp/ 2>/dev/null ; cd webp && find -type f -iregex '.*\.\(png\|bmp\|jpeg\|jpg\)$' | xargs -P8 -I{} -exec sh -c 'for f; do ffmpeg -i "$f" -c:v libwebp -lossless 0 -compression_level 6 -qscale 80 -preset default -y "${f%.*}".webp ; done' _ {} ; find -type f -iregex '.*\.\(png\|bmp\|jpeg\|jpg\)$' -exec rm -rf {} +
I know this might look intimidating, but for 99% of cases, the only thing you'll have to worry about is the very fist part of the script, the part that says:
input_folder="/home/user/Pictures/"
Here you will only change/update the direction of the folder you want to work with… So for example, let's say the images you want to convert are in
"/home/user/GraphicDesign/Work/Assets/Project Photos/"
, well the script will begin with:input_folder="/home/user/GraphicDesign/Work/Assets/Project Photos/"
And the rest is exactly the same, so everything will look like this:
input_folder="/home/user/GraphicDesign/Work/Assets/Project Photos/" ; cd "$input_folder" && mkdir -p webp && cp -u "$input_folder"/* "$input_folder"/webp/ 2>/dev/null ; cd webp && find -type f -iregex '.*\.\(png\|bmp\|jpeg\|jpg\)$' | xargs -P8 -I{} -exec sh -c 'for f; do ffmpeg -i "$f" -c:v libwebp -lossless 0 -compression_level 6 -qscale 80 -preset default -y "${f%.*}".webp ; done' _ {} ; find -type f -iregex '.*\.\(png\|bmp\|jpeg\|jpg\)$' -exec rm -rf {} +
You'll just have to paste the script in the terminal, hit Enter and watch the magic happen.
As an example, I have a folder with several photos for a web page, some images are in JPG and some in PNG; the folder is 756MB… When I run the script, it creates a "webp" sub-folder and puts the converted images inside. That folder contains all the images but now in WebP format (without touching the original images outside, don't worry).
Now the webp folder is just 48MB!
Like I said, this script should work in 99% of cases.
I can explain everything in the script so you can adapt it to your need, but to keep this post digestable, I'll leave it here.
Feel free to comment or ask any question!