File size: 1,510 Bytes
c2d451f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#!/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"