-
Notifications
You must be signed in to change notification settings - Fork 1
/
convert_any2srgb
executable file
·94 lines (82 loc) · 2.56 KB
/
convert_any2srgb
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
#!/bin/bash
#
# convert any image (essentially from AdobeRGB or CMYK) to sRGB jpg
# and optionally resize
#
#
# From a IM Forum discussion...
# http://www.imagemagick.org/discourse-server/viewtopic.php?f=1&t=16464&start=15
#
# Curtisy of Albert25 -- 26 June 2010
#
###
# paths to profile file locations
srgb=/docs/photos/icc/sRGB.icm
cmyk=/docs/photos/icc/USWebCoatedSWOP.icc
# defaults
outdir=.
debug=
# read options
while getopts "dvho:s:a:" opt
do
case $opt in
"d|v" ) debug=1;; # verbose is same as debug for now
"s" ) size=$OPTARG;;
"o" ) outdir=$OPTARG;;
"a" ) add_options=$OPTARG;;
"h" ) cat <<END_HELP;
Usage: $0 [options] FILE [FILE...]
Options:
-h Print this help
-d Print debugging info while processing
-v Be verbose (same as -d for now)
-s nnn[xhhh] Specify new width in pixels or new size as widthxheight (ie -w 640x480)
-a "im options" Additional options for IM
-o dir Output directory
END_HELP
exit 0;;
esac
done
shift $(($OPTIND - 1))
if [ -n "$size" ]; then
resize="-resize $size"
else
resize=
fi
shopt -s nullglob
for f in "$@"
do
# skip if not jpeg or tiff
#[[ "$f" =~ '\.(jpe?g|tiff?)$' ]] || continue
dir=$(dirname "$f")
name=$(basename "$f")
base=${name%.*}
profile="/tmp/$base.icc"
out="$outdir/$base-x$size.jpg"
# extract possible color profile
[ -n "$debug" ] && echo " extracting profile $f"
convert "$f" "$profile" 2>/dev/null
[ -n "$debug" ] && echo " resize = $resize"
if [ -s "$profile" ]; then
# we have an embedded profile, so ImageMagick will use that anyway
[ -n "$debug" ] && echo " convert with embedded profile $f"
convert "$f" -profile "$srgb" +profile '*' $resize $add_options "$out" \
&& echo -e "\tOK $out"
else
# no embedded profile in source
if identify -format "%r" "$f" | grep CMYK >/dev/null ; then
# CMYK file without embedded profile
[ -n "$debug" ] && echo " convert cmyk without embedded profile $f"
convert "$f" -profile "$cmyk" -profile "$srgb" +profile '*' $resize $add_options "$out" \
&& echo -e "\tOK $out"
# alternative would be
#convert "$f" -colorspace RGB -resize $size "$out"
else
# No profile and not CMYK. Probably already in RGB. Just resize
[ -n "$debug" ] && echo " resizing only $f"
convert "$f" $resize $add_options "$out" \
&& echo -e "\tOK $out"
fi
fi
[ -f "$profile" ] && rm "$profile"
done