webp
is very common nowadays but usually screenshots and other images are available in a different format, e.g. jpg or png. Converting a single image is easy using cwebp
. If all files in a folder should be converted, a shell script or command is needed.
Inline it could be done using the following:
for file in *.png; do if [ -f "$file" ]; then cwebp -lossless $file -o "${file%.*}.webp" -quiet ; fi; done
for file in *.jpg; do if [ -f "$file" ]; then cwebp -q 90 $file -o "${file%.*}.webp" -quiet ; fi; done
If a bit more control is needed, a proper bash script is the solution:
#!/bin/bash
image_quality=90
valid_img_file_extensions=("png" "tif" "tiff" "jpg" "jpeg")
folder_path=""
lossless_mode=false
for i in "$@"
do
case $i in
-p=*|--folderpath=*)
folder_path="${i#*=}"
;;
-ext=*|--file-extension=*)
valid_img_file_extensions=("${i#*=}")
;;
-q=*|--compression-quality=*)
image_quality="${i#*=}"
;;
-l*|--lossless*)
lossless_mode=true
;;
esac
done
for file in "$folder_path"*
do
if [ -f $file ]
then
ext=${file#*.}
if [[ $valid_img_file_extensions =~ (^|[[:space:]])"$ext"($|[[:space:]]) ]]
then
output_fn="${file%.*}.webp"
if $lossless_mode
then
cwebp -lossless $file -o $output_fn -quiet
else
cwebp -q $image_quality $file -o $output_fn -quiet
fi
fi
fi
done
The shell script can also be found here.