diff --git a/compress_images.sh b/compress_images.sh new file mode 100644 index 0000000..07eb54a --- /dev/null +++ b/compress_images.sh @@ -0,0 +1,29 @@ +#!/bin/bash + +# Directory containing the images +directory="./images" + +# Compression level (default to 0 if not provided) +compression_level=${1:-0} + +# Create output directory, overwrite if it already exists +output_directory="${directory}/webp" +rm -rf "$output_directory" +mkdir -p "$output_directory" + +# Iterate through each file in the directory +for file in "$directory"/*.{jpg,jpeg,png,bmp,tiff}; do + # Skip if no matching files are found + [ -e "$file" ] || continue + + # Extract the filename without extension + filename=$(basename "$file") + filename_no_ext="${filename%.*}" + + # Convert the file to WebP with specified compression level + cwebp -q "$compression_level" "$file" -o "$output_directory/${filename_no_ext}.webp" + + echo "Converted: $file -> $output_directory/${filename_no_ext}.webp" +done + +echo "All images have been converted to WebP with compression level $compression_level."