-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathdiff-image
executable file
·284 lines (249 loc) · 6.77 KB
/
diff-image
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
#!/bin/bash
set -euo pipefail
usage()
{
echo "Usage: $0 [<options>] <file1> <file2>"
echo
echo "Options:"
echo " -h Print this help."
echo " -b <color> Use this as the background color; defaults to white."
echo " -c <color> Highlight differences with this color; defaults to red."
echo " -e Show Exif differences only; don't compare the image data."
echo " -f <fuzz> Use the specified percentage of fuzz. Defaults to "
echo " 5% for JPEGs, zero otherwise."
echo " -n <name> The name to give the first file."
echo " -N <name> The name to give the second file."
echo " -o <path> Output pathname to save diff instead of showing it"
echo
}
backgroundcolor=
color=
exif_only=false
fuzz=
name1=
name2=
outputPath=
while getopts "hb:c:ef:n:N:o:" opt
do
case "$opt" in
h)
usage
exit 0
;;
b)
backgroundcolor="$OPTARG"
;;
c)
color="$OPTARG"
;;
e)
exif_only=true
;;
f)
fuzz="$OPTARG"
;;
n)
name1="$OPTARG"
;;
N)
name2="$OPTARG"
;;
o)
outputPath="$OPTARG"
;;
esac
done
shift $(( OPTIND - 1 ))
if [ -z "${1-}" ] || [ -z "${2-}" ]
then
usage
exit 1
fi
f1="$1"
f2="$2"
if [[ "$f1" != '/dev/null' ]] && [[ ! -f "$f1" ]]
then
echo "$f1: No such file." >&2
exit 1
fi
if [[ -d "$f2" ]]
then
f=$(basename "$f1")
f2="$f2/$f"
fi
if [[ "$f2" != '/dev/null' ]] && [[ ! -f "$f2" ]]
then
echo "$f2: No such file." >&2
usage
exit 1
fi
if [[ -z "$name1" ]]
then
name1="$f1"
fi
if [[ -z "$name2" ]]
then
name2="$f2"
fi
ext="${name1##*.}"
if diff "$f1" "$f2" >/dev/null
then
exit 0
fi
exif()
{
if [[ "$1" = /dev/null ]]
then
echo /dev/null
return
fi
local b="$(basename "$1")"
local d="$(mktemp -t "$b.XXXXXX")"
exiftool "$1" | grep -v 'File Name' | \
grep -v 'Directory' | \
grep -v 'ExifTool Version Number' | \
grep -v 'File Inode Change' | \
grep -v 'File Access Date/Time' | \
grep -v 'File Modification Date/Time' | \
grep -v 'File Permissions' | \
grep -v 'File Type Extension' | \
sort \
>"$d"
echo "$d"
}
diff_clean_names()
{
diff -u "$1" --label "$name1" "$2" --label "$name2" || true
}
exifdiff=
if which exiftool > /dev/null
then
d1="$(exif "$f1")"
d2="$(exif "$f2")"
diff_clean_names "$d1" "$d2"
set +e
diff -q "$d1" "$d2" >/dev/null
exifdiff=$?
set -e
else
diff_clean_names "$f1" "$f2"
fi
if $exif_only
then
exit 0
fi
if \
! which compare > /dev/null || \
! which montage > /dev/null
then
if which gm > /dev/null
then
echo 'GraphicsMagick is installed, but graphicsmagick-imagemagick-compat missing.' >&2
echo 'Alternatively the minimum required compatibility links can be installed' >&2
echo 'by running:' >&2
echo ' sudo ln -s gm /usr/bin/compare' >&2
echo ' sudo ln -s gm /usr/bin/montage' >&2
else
echo 'ImageMagick or GraphicsMagick is not installed.' >&2
fi
exit 1
fi
if [[ $exifdiff = 0 ]] && compare "$f1" "$f2" /dev/null
then
exit 0
fi
bn="$(basename "$f1")"
destfile="$(mktemp -t "$bn.XXXXXX").png"
if [ -z "$fuzz" ] && ( [ "$ext" = "jpeg" ] || [ "$ext" = "jpg" ] )
then
fuzz='5'
fi
backgroundcolor_flag=
if [ -n "$backgroundcolor" ]
then
backgroundcolor_flag="-background $backgroundcolor"
fi
color_flag=
if [ -n "$color" ]
then
color_flag="-highlight-color $color"
fi
fuzz_flag=
if [ -n "$fuzz" ]
then
fuzz_flag="-fuzz $fuzz%"
fi
density_flag=
do_compare()
{
if which gm > /dev/null
then
echo "NOTICE: GraphicsMagick does not support 'compare -fuzz', so omitting it"
compare $density_flag $color_flag $backgroundcolor_flag -file png:- "$f1" "$f2" | \
montage $density_flag -geometry +4+4 $backgroundcolor_flag "$f1" - "$f2" png:- >"$destfile" 2>/dev/null || true
else
compare $density_flag $color_flag $fuzz_flag $backgroundcolor_flag "$f1" "$f2" png:- | \
montage $density_flag -geometry +4+4 $backgroundcolor_flag "$f1" - "$f2" png:- >"$destfile" 2>/dev/null || true
fi
}
if which xdg-open > /dev/null
then
# Get width and height of each input image.
f1_width="$(exiftool -S -ImageWidth "$f1" | cut -d' ' -f2)"
f2_width="$(exiftool -S -ImageWidth "$f2" | cut -d' ' -f2)"
f1_height="$(exiftool -S -ImageHeight "$f1" | cut -d' ' -f2)"
f2_height="$(exiftool -S -ImageHeight "$f2" | cut -d' ' -f2)"
# find the max of each.
if (( $(echo "$f1_width > $f2_width" |bc -l) )); then
max_file_width=$f1_width
else
max_file_width=$f2_width
fi
if (( $(echo "$f1_height > $f2_height" |bc -l) )); then
max_file_height=$f1_height
else
max_file_height=$f2_height
fi
screen_width="$(xdpyinfo | grep dimensions | sed -e 's/.* \([^ ]*\)x\([^ ]*\) pixels.*/\1/')"
screen_height="$(xdpyinfo | grep dimensions | sed -e 's/.* \([^ ]*\)x\([^ ]*\) pixels.*/\2/')"
resolution_width="$(xdpyinfo | grep resolution | sed -e 's/.* \([^ ]*\)x\([^ ]*\) dots per inch.*/\1/')"
resolution_height="$(xdpyinfo | grep resolution | sed -e 's/.* \([^ ]*\)x\([^ ]*\) dots per inch.*/\2/')"
# Assume that the combined size will be the same as the maximum of
# each. Add 100 pixels on each side for the window borders.
montage_width=$( echo "$f1_width + $max_file_width + $f2_width + 100" |bc -l )
montage_height=$( echo "$f1_height + $max_file_height + $f2_height + 100" |bc -l )
# Select the most limiting (lowest) density.
if (( $(echo "($resolution_width / $montage_width * $screen_width) < ($resolution_height / $montage_height * $screen_height)" |bc -l) )); then
density=$( echo "$resolution_width / $montage_width * $screen_width" |bc -l )
else
density=$( echo "$resolution_height / $montage_height * $screen_height" |bc -l )
fi
# If the density needed is less than either of the inputs, use it.
if (( $(echo "$density < $resolution_width || $density < $resolution_height" |bc -l) )); then
density_flag="-density $density"
fi
do_compare
if [ -n "$outputPath" ]
then
echo "Copy diff image to $outputPath"
cp "$destfile" "$outputPath"
else
xdg-open "$destfile"
fi
else
w=$(exiftool -p '$ImageWidth' "$f1" || true)
if [[ $w -ge 10000 ]]
then
cp "$f1" "$destfile"
exec open "$destfile" "$f2"
else
do_compare
if [ -n "$outputPath" ]
then
echo "Copy diff image to $outputPath"
cp "$destfile" "$outputPath"
else
exec open "$destfile"
fi
fi
fi