A couple of years ago webp
was the defacto standard for images on websites. However, it is slowly going to be replaced by avif to serve images and graphics on websites. Converting a single image is easy using avifenc
and therefore it can easily be used in a shell script.
Inline it could be done using the following:
for file in *.png; do if [ -f "$file" ]; then avifenc --lossless $file "${file%.*}.avif" ; fi; done
for file in *.jpg; do if [ -f "$file" ]; then avifenc --speed 2 $file "${file%.*}.avif" ; fi; done
If a bit more control is needed, a proper bash script is the solution:
#!/bin/bash
speed=2
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=*)
speed="${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%.*}.avif"
if $lossless_mode
then
avifenc --lossless $file $output_fn
else
avifenc --speed $speed $file $output_fn
fi
fi
fi
done
The shell script can also be found here.