-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathconv_webp
executable file
·52 lines (42 loc) · 1.27 KB
/
conv_webp
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
#!/bin/bash
# Check if cwebp is installed
if ! command -v cwebp &> /dev/null; then
echo "cwebp could not be found. Please install it first."
exit 1
fi
# Function to convert images in a directory
convert_images() {
local dir="$1"
# Loop through all image files in the current directory
for img in "$dir"/*.jpg "$dir"/*.jpeg "$dir"/*.png "$dir"/*.gif \
"$dir"/*.JPG "$dir"/*.JPEG "$dir"/*.PNG "$dir"/*.GIF; do
# Check if there are no image files
if [ ! -e "$img" ]; then
continue
fi
# Define the output file name
output="${img%.*}.webp"
# Encode the image to WebP format
cwebp "$img" -o "$output"
# Check if the encoding was successful
if [ $? -eq 0 ]; then
echo "Converted $img to $output"
else
echo "Failed to convert $img"
fi
done
}
# Function to recursively process directories
process_directory() {
local dir="$1"
# Convert images in current directory
convert_images "$dir"
# Process subdirectories
for subdir in "$dir"/*/ ; do
if [ -d "$subdir" ]; then
process_directory "$subdir"
fi
done
}
# Start processing from current directory
process_directory "."