It is always good to serve optimized & quality images on your website. The WebP format is always recommended for everything web.
The aim of this script is to convert all your images in batch & organize them to a new directory.
WebP is the format developed with web in mind. It provides many performance benefits while serving the assets for your website.
Reference: web.dev
The script uses the WebP library provided by Google for the conversion process.
You are required to copy the provided script to the directory where all your images are being stored. Though not neccesary but suggested, that the directory should contain only the image files.
On running, the script will scan the current directory for images with extensions of '.jpg', '.jpeg' & '.png'. Next, using the following bash command, the script will convert the every selected image to '.webp' format.
Command: cwebp -q 80 image.jpg -o image.webp
where, 80 = typical output quality.
Quality can be modified by editing the quality
variable in the script.
Once converted, the images will be organized to a new directory named 'webp'.
The name of the directory can be modified by editing the dir_name
variable in the script.
NOTE: Converted images will have the same name as they had before conversion.
The script is smart enough to:
- Detect for the missing images after conversion & restore them accordingly.
- Detect if the missing images are present in the original directory, it moves them accordingly and thus save you time by not converting existing images again.
More features are in progress.
- Install the latest & stable version i.e., 3.8.1 of Python.
- Download the WebP library provided by Google. You can find the latest version i.e., 1.1.0 at bottom of the page.
- Extract the compressed folder & add path of the bin directory to your system variables.
Path will look something like this:
..\libwebp-1.1.0-windows-x64\bin
- Enter
cwebp -version
in CLI, which will return the version number. This indicates a successful installation. - You are ready to copy the script & use it.
I hope you find this script useful, cheers!
Code Plus Coffee