-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathimage-convert
executable file
·156 lines (130 loc) · 3.62 KB
/
image-convert
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
#!/bin/bash
set -euo pipefail
usage() {
cat >&2 <<EOM
usage: $(basename "$0") [options] FILE [OUTPUT]
Batch convert image files, with various options.
To choose the output filename, you must either specify the OUTPUT file or the
output extension with -e/--ext.
The output extension will also control the output image format used, if you
want to convert from one format to another.
See also: jhead(1), convert(1), ImageMagick(1)
Options:
-h, --help Display this help text
-q, --quality NUM Use JPEG quality level NUM
-r, --resize RES Resize image to WxH resolution
-f, --force Overwrite existing output files
-e, --ext EXT Use EXT as the output file extension (and image format)
-o, --out-dir DIR Write output files to DIR
--mtime Set mtime of output files to match input
Examples:
# Convert input.JPG quality and resolution, save as output.jpg
image-convert -q 90 -r 640x480 -e jpg input.JPG
# Convert a whole directory of files in parallel
parallel image-convert -q 90 -e jpg -- *.JPG
# Write files to directory \`output/'
parallel image-convert -q 90 -o output/ -- input/*.jpg
# Convert png files to jpg
parallel image-convert -q 95 -o output/ -e jpg -- *.png
# Convert png files to lossy webp
parallel image-convert -q 95 -o output/ -e webp -- *.png
EOM
}
run() {
echo >&2 "+ $*"
"$@"
}
opts=()
force=
out_dir=
out_ext=
mtime=
while [ $# -gt 0 ] && [[ $1 == -* ]]; do
case "$1" in
-h|--help)
usage
exit
;;
-e|--ext)
out_ext="$2"
shift
;;
-q|--quality)
opts+=(-quality "$2")
shift
;;
-r|--resize)
opts+=(-resize "$2")
shift
;;
-f|--force)
force=1
;;
-o|--out-directory)
out_dir="$2"
shift
;;
--mtime)
mtime=1
;;
*)
usage
echo >&2 "Unknown option '$1'"
exit 1
;;
esac
shift
done
case $# in
1)
input="$1"
# split on final '.'
ext="${input##*.}"
basename="${input%.*}"
if [ -n "$out_dir" ]; then
# use out_dir as output directory
mkdir -vp "$out_dir"
if [ -n "$out_ext" ]; then
# user-specified output extension
output="$out_dir/$basename.$out_ext"
else
# default to input extension for output
output="$out_dir/$basename.$ext"
fi
else
# must specify output extension if output directory is not given
if [ -z "$out_ext" ]; then
usage
echo >&2 "error: Where should output go? Must pass OUTPUT, --out-dir, or --ext"
exit 2
fi
output="$(dirname "$input")/$basename.$out_ext"
fi
;;
2)
if [ -n "$out_dir" ]; then
usage
echo >&2 "error: cannot pass OUTPUT and --out-dir"
exit 1
fi
if [ -n "$out_ext" ]; then
usage
echo >&2 "error: cannot pass OUTPUT and --ext"
exit 1
fi
input="$1"
output="$2"
;;
*)
usage
exit 1
;;
esac
if [ -z "$force" ] && [ -e "$output" ]; then
echo >&2 "error: output file already exists: '$output'"
exit 3
fi
run convert "${opts[@]}" "$input" "$output"
if [ -n "$mtime" ]; then
touch --no-create --reference "$input" "$output"
fi