-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconvert-to-srgb
executable file
·110 lines (92 loc) · 2.42 KB
/
convert-to-srgb
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
#!/bin/bash
set -eo pipefail
SRGB_ICC_PROFILE=~/.local/share/color/icc/sRGB-IEC61966-2.1.icc
throw() {
printf '%s: %s\n' "${0##*/}" "$1" >&2
return 1
}
if [[ $# == 0 || $1 =~ ^(--help|-h)$ ]]; then
cat >&2 <<EOF
Usage: convert-to-srgb [-a] <images...>
For each input image, if it contains a non-sRGB color profile, extracts the
profile with exiftool (as imagemagick sometimes fails to recognize embedded ICC
profiles) and converts it to sRGB using the ICC profile at:
$SRGB_ICC_PROFILE
Output images are saved as PNGs alongside the inputs, with the original
extension changed to '.srgb.png'.
Options:
-a Convert all images to PNG, with the '.srgb.png' suffix, even if they're
already sRGB or PNG. Useful to avoid separate code paths in scripts.
-x Exit with a non-zero exit code if an image does not need converting.
Mutually exclusive with -a.
EOF
exit 1
fi
paths=()
all=
fail=
while (( $# > 0 )); do
case $1 in
--)
shift
paths+=("$@")
break
;;
-a)
all=1
;;
-x)
fail=1
;;
-*)
throw "unknown option: $1"
;;
*)
paths+=("$1")
;;
esac
shift
done
skip() {
local reason=$1
local input=$2
local output=$3
if [[ ! $all ]]; then
printf ' %s, skipping.\n' "$reason"
if [[ $fail ]]; then
exit 1
fi
return
elif [[ $input == *.png ]]; then
printf ' %s, copying as-is...\n' "$reason"
cp "$input" "$output"
else
printf ' %s, converting to png...\n' "$reason"
magick "$input" "$output"
fi
printf ' Saved \e[35m%s\e[m\n' "$output"
}
for input in "${paths[@]}"; do
printf '\e[32m%s\e[m\n' "$input"
output="${input%.*}.srgb.png"
# Check for non-sRGB color profile
profile_desc=$(exiftool -m -b -ICC_Profile:ProfileDescription "$input")
if [[ $profile_desc ]]; then
printf ' Color profile: \e[36m%s\e[m\n' "$profile_desc"
if [[ $profile_desc == sRGB* ]]; then
skip 'Input already sRGB' "$input" "$output"
continue
fi
else
skip 'Input does not have an embedded color profile' "$input" "$output"
continue
fi
# Extract embedded ICC profile
echo ' Extracting color profile...'
exiftool -q -m -icc_profile -b -w! icc "$input"
# Convert to sRGB
echo ' Converting to sRGB...'
magick "$input" -profile "${input%.*}.icc" -profile "$SRGB_ICC_PROFILE" "$output"
printf ' Saved \e[35m%s\e[m\n' "$output"
rm "${input%.*}.icc"
done