-
Notifications
You must be signed in to change notification settings - Fork 108
/
Copy pathoptimize-img.sh
executable file
·82 lines (70 loc) · 1.85 KB
/
optimize-img.sh
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#!/usr/bin/env bash
set -e
src="$1"
if ! [[ -f "$src" ]]; then
printf -- "Usage: %s file.{png,webp}\n" "$0" >&2
exit 1
fi
msg() {
printf -- '%s: %s\n' "$src" "$*"
}
getsize() {
local out="$(wc -c "$1")"
printf -- '%s' "${out%% *}"
}
makewebp() {
local input="$1"
local output="$2"
cwebp \
-preset icon \
-z 9 \
-lossless \
-q 100 \
-m 6 \
"$input" -o "$output" 2>&1 | while read -r line; do msg "[cwebp] $line"; done
}
fileid="$(file "$src")"
orig_size="$(getsize "$src")"
if [[ "$fileid" == *" Web/P image"* ]]; then
if [[ "$fileid" == *" YUV color"* ]]; then
msg "lossy WebP detected, will not process"
exit
fi
msg "recompressing..."
makewebp "$src" "$src~"
new_size="$(getsize "$src~")"
msg "old: $orig_size bytes"
msg "new: $new_size bytes"
if [[ "$new_size" -lt "$orig_size" ]]; then
msg "keeping new version"
mv "$src~" "$src"
else
msg "keeping original version"
rm -f "$src~"
fi
elif [[ "$fileid" == *" PNG image data"* ]]; then
msg "leanifying..."
leanify -vvv "$src" 2>&1 | while read -r line; do msg "[leanify] $line"; done
png_size="$(getsize "$src")"
id="$(identify "$src")"
if [[ "$id" == *" 16-bit "* ]]; then
msg "16bpp image, WebP conversion skipped"
else
msg "converting to WebP..."
webp="${src%%png}webp"
makewebp "$src" "$webp"
webp_size="$(getsize "$webp")"
msg "PNG: $png_size bytes"
msg "WebP: $webp_size bytes"
if [[ "$png_size" -lt "$webp_size" ]]; then
msg "keeping PNG ('$src'; $png_size bytes)"
rm -f "$webp"
else
msg "keeping WebP ('$webp'; $webp_size bytes)"
rm -f "$src"
fi
fi
else
msg "unhandled file type" >&2
exit 1
fi