Jerusalem-Streetscapes / convert-to-webp.sh
danielrosehill's picture
commit
c2d451f
#!/bin/bash
# Script to convert all existing PNG images to WebP format
# Maintains the same numbering scheme
set -e
IMAGES_DIR="./images"
# Check if directory exists
if [ ! -d "$IMAGES_DIR" ]; then
echo "Error: $IMAGES_DIR directory not found"
exit 1
fi
# Check for conversion tool
if ! command -v cwebp &> /dev/null && ! command -v convert &> /dev/null; then
echo "Error: Neither cwebp nor ImageMagick (convert) found. Please install one of them."
exit 1
fi
echo "Converting all PNG images to WebP format..."
CONVERTED=0
SKIPPED=0
for img in "$IMAGES_DIR"/*.png; do
# Skip if no PNG files found
[ -f "$img" ] || continue
# Get filename without extension
filename=$(basename "$img" .png)
output="${IMAGES_DIR}/${filename}.webp"
# Skip if WebP already exists
if [ -f "$output" ]; then
echo "Skipping $filename.png (WebP already exists)"
SKIPPED=$((SKIPPED + 1))
continue
fi
echo "Converting: $filename.png -> $filename.webp"
# Convert to WebP
if command -v cwebp &> /dev/null; then
cwebp -q 85 "$img" -o "$output" 2>/dev/null
else
convert "$img" -quality 85 "$output"
fi
# Remove original PNG after successful conversion
if [ -f "$output" ]; then
rm "$img"
CONVERTED=$((CONVERTED + 1))
else
echo "Error: Failed to convert $filename.png"
fi
done
echo ""
echo "Conversion complete!"
echo "Images converted: $CONVERTED"
echo "Images skipped: $SKIPPED"