-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwebp
More file actions
executable file
·53 lines (44 loc) · 1.22 KB
/
Copy pathwebp
File metadata and controls
executable file
·53 lines (44 loc) · 1.22 KB
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
#!/usr/bin/bash
# REQUIRE IMAGEMAGICK INSTALLED
# sudo apt-get install imagemagick
if ! command -v convert &> /dev/null; then
echo "ImageMagick is not installed. Please install it first."
exit 1
fi
# Default quality and delete option
quality=85
delete_files=false
# Parse options
while getopts "q:d" opt; do
case $opt in
q)
quality=$OPTARG
;;
d)
delete_files=true
;;
*)
echo "Usage: $0 [-q quality] [-d]"
exit 1
;;
esac
done
shift $((OPTIND -1))
convert_to_webp() {
local input_file=$1
local quality=$2
local output_file="${input_file%.*}.webp"
if convert -quality "$quality" "$input_file" "$output_file"; then
echo "Converted $input_file to $output_file"
else
echo "Failed to convert $input_file"
fi
}
export -f convert_to_webp
# Find and convert all PNG, JPG, and JPEG files in the current directory and its subdirectories
find . -type f \( -name "*.png" -o -name "*.jpg" -o -name "*.jpeg" \) -exec bash -c 'convert_to_webp "$0" '"$quality" {} \;
# Optionally, remove the original files
if [ "$delete_files" = true ]; then
echo "Deleting all the original files..."
find . -type f \( -name "*.png" -o -name "*.jpg" -o -name "*.jpeg" \) -exec rm -f {} \;
fi