forked from Marwe/dalliclick
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dalliclick.sh
executable file
·315 lines (287 loc) · 9.87 KB
/
dalliclick.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
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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
#!/bin/bash
#####################################################################
# Program : dalliklick.sh
#
# Version : 0.1
# Date : 12/2008
# Author : Martin Weis
#
# Inputs : One image that should be "dalli-klicked"
#
# Notes : The program creates a "background" image (see -b for your own one)
# and combines this with the given image in a way, that several output
# images are created, which show more and more parts of the original image,
# until the full image is visible.
# The program is called dalliklick, because there was a german TV series in
# the seventies (Dalli dalli), that called this game Dalli Klick.
# You will need an ImageMagick version greater than 6.4.3. Get it here, and define
# the paths to CONVERT_BIN and COMPOSITE_BIN:
# http://www.imagemagick.org/
# Result images are written to the working directory, if -t is given
#
#####################################################################
# system parameters
# if you have a newer version of convert, define the executable here
#CONVERT_BIN="${HOME}/local/imagemagick-6.4.7-8/bin/convert"
CONVERT_BIN=`which convert`
#COMPOSITE_BIN="${HOME}/local/imagemagick-6.4.7-8/bin/composite"
COMPOSITE_BIN=`which composite`
# Global variables
SCRIPTNAME=$(basename $0)
EXIT_SUCCESS=0
EXIT_FAILURE=1
EXIT_ERROR=2
EXIT_PARAM_ERROR=5
EXIT_BUG=10
# define defaults for option values
# default width and height of the resulting image
#####################
# true = 0, false = 1
#####################
VERBOSE=0
WIDTH=1024
HEIGHT=766
# generate a mask image:
GENERATE_MASK=1
NUMSEGMENTS=10
RESULTIMAGEBASE_GIVEN=1
OUTPUTFILEFORMAT=png
# do all output operations in a temporary dir? where should this be located?
GENTMPDIR=0
# OUTPUTDIR=`pwd`
# mask image with several gray valued (1,2,3,...) areas
MASKIMAGENAME="tmp_voronoi_template.png"
# black and white image (transparency info)
GRAYMASKDEFAULTFILENAME="tmp_maskimage.png"
# generate a background image?
GENERATE_BACKGROUND=1
BACKGROUNDIMAGENAME="tmp_background.png"
RESIZEIMAGENAME="tmp_resized.png"
FILLUPCOLOR="black"
# functions
function usage {
echo "This script creates partially occluded image versions of the input image"
echo "Usage: $SCRIPTNAME <Options> imagefile..."
echo "Options:"
echo "-h Show this help"
echo "-v Increase verbosity (more details on actions)"
echo "-n Segments Number of segments, max. 255 (Default 10)"
echo "-o Name Output image base name (out -> out_NNN.png)"
echo "-b File Background (canvas) image. The output images will have exactly"
echo " the size of the background image."
echo " If this is not specified, an artifical background image will be"
echo " generated."
echo "-B color Color for filling up images in wrong ratio (only relevant with -b)"
echo "-m maskimage User specific mask image with label gray values 1,2,3,..."
echo " If this is not specified a random mask image will be generated."
echo "-t Do not generate temporary directory (working dir is used)"
echo "-O format Format of output files (Default: png)"
[[ $# -eq 1 ]] && exit $1 || exit $EXIT_FAILURE
}
generate_voronoi_template(){
# check for inputs: width, height, number of segments
if [ -z $1 ]
then
WID=$WIDTH
else
WID="$1"
fi
if [ -z $2 ]
then
HEI=$HEIGHT
else
HEI="$2"
fi
if [ -z $3 ]
then
NSEG=10
else
NSEG="$3"
fi
if [ $NSEG -gt 255 ]
then
echo "warning: generate_voronoi_template() number of segments greater 255 ($NSEG), setting to 255"
NSEG=255
fi
if [ $VERBOSE -ne 0 ]; then echo "generate_voronoi_template() WID: $WID, HEI: $HEI, NSEG: $NSEG"; fi
# see http://www.imagemagick.org/Usage/canvas/#voronoi
CMD="$CONVERT_BIN -size ${WID}x${HEI} "
CMD="$CMD -colors 256 -colorspace GRAY "
CMD="$CMD xc:black -sparse-color Voronoi '"
for i in `seq -w 1 1 $NSEG`
do
# generate random postions within image coordinate range
# imagemagick can do this ;-)
#RANDX=$RANDOM
#let "RANDX %= $WID"
#RANDY=$RANDOM
#let "RANDY %= $HEI"
#CMD="$CMD $RANDX,$RANDY rgb($i,$i,$i)"
CMD="$CMD %[fx:rand()*w],%[fx:rand()*h] rgb($i,$i,$i)"
done
CMD="$CMD ' $TMPMASKIMAGENAME"
if [ $VERBOSE -ne 0 ] ; then echo $CMD; fi
eval $CMD
if [ $? -ne 0 ] ; then echo "could not create maskimage '$TMPMASKIMAGENAME'"; fi
}
function dallicleanup(){
#if [ $VERBOSE -ne 0 ] ; then echo "cleaning up bg"; fi
# cleanup *generated* files
if [ $GENERATE_BACKGROUND -ne 0 ] ; then
# # remove 'starting' canvas
rm -f "$BACKGROUNDIMAGE"
fi
#if [ $VERBOSE -ne 0 ] ; then echo "cleaning up mask"; fi
if [ $GENERATE_MASK -ne 0 ] ; then
# # remove mask image
rm -f "$TMPMASKIMAGENAME"
fi
if [ "$INPUTIMAGERESIZED" ]; then
rm -f "$INPUTIMAGERESIZED"
fi
rm -f "$GRAYMASKFILE"
# cleanup the temporary directory, if that was created
#if [ $VERBOSE -ne 0 ] ; then echo "cleaning up tmpdir"; fi
if [ $GENTMPDIR -eq 0 ] ; then
if [ -d "$TMPDIR" ] ; then
rm -rf "$TMPDIR"
fi
fi
}
function failbail(){
echo "sorry, failed: $1"
echo "stopping"
dallicleanup
exit $EXIT_FAILURE
}
# check imagemagick version, might only work for one-digit numbers
CONVERT_ACCEPTABLE_VER="6.4.3"
CONVERT_VERSION=`$CONVERT_BIN -version | head -n 1 | cut -d ' ' -f 3`
if [[ "$CONVERT_VERSION" < "$CONVERT_ACCEPTABLE_VER" ]]; then
failbail "convert version $CONVERT_VERSION is too old, at least $CONVERT_ACCEPTABLE_VER of ImageMagick is required"
fi
COMPOSITE_ACCEPTABLE_VER="6.4.3"
COMPOSITE_VERSION=`$COMPOSITE_BIN -version | head -n 1 | cut -d ' ' -f 3`
if [[ "$COMPOSITE_VERSION" < "$COMPOSITE_ACCEPTABLE_VER" ]]; then
failbail "composite version $COMPOSITE_VERSION is too old, at least $COMPOSITE_ACCEPTABLE_VER of ImageMagick is required"
fi
# parse options
# Option -h (help) should always be there
# if you have an option argument to be parsed use ':' after option
while getopts ':n:i:b:m:o:O:B:vht' OPTION ; do
case $OPTION in
v) VERBOSE=$((VERBOSE+1))
;;
h) usage $EXIT_SUCCESS
;;
n) NUMSEGMENTS="$OPTARG"
;;
m) MASKIMAGE="$OPTARG"
GENERATE_MASK=0
;;
o) RESULTIMAGEBASE="$OPTARG"
RESULTIMAGEBASE_GIVEN=0
echo "given basename: "$RESULTIMAGEBASE;
;;
O) OUTPUTFILEFORMAT="$OPTARG"
;;
b) BACKGROUNDIMAGENAME="$OPTARG"
GENERATE_BACKGROUND=0
;;
B) FILLUPCOLOR="$OPTARG"
;;
t) GENTMPDIR=1
;;
\?) echo "Unknown option \"-$OPTARG\"." >&2
usage $EXIT_ERROR
;;
:) echo "Option \"-$OPTARG\" given without argument." >&2
usage $EXIT_ERROR
;;
*) echo "Parsing of options failed, sorry (this is a bug...)" >&2
usage $EXIT_BUG
;;
esac
done
if [ $GENTMPDIR -eq 0 ] ; then
TMPDIR=`mktemp -d dalliklick.XXXXX`
else
TMPDIR="."
fi
# skip used options
shift $(( OPTIND - 1 ))
# test for number of arguments
if (( $# < 1 )) ; then
echo "Missing input image." >&2
usage $EXIT_ERROR
fi
INIMGCNT=""
for INPUTIMAGE in "$@"
do
if [ $VERBOSE -ne 0 ] ; then echo "processing $INPUTIMAGE"; fi
if [ $GENERATE_BACKGROUND -ne 0 ] ; then
WIDTH=`identify -format "%w" "$INPUTIMAGE"`
HEIGHT=`identify -format "%h" "$INPUTIMAGE"`
if [ -z "$WIDTH" -o -z "$HEIGHT" ] ; then
failbail "cannot identify size of image $INPUTIMAGE" ;
fi
# do this in the temporary directory
BACKGROUNDIMAGE="$TMPDIR/$BACKGROUNDIMAGENAME"
# generate 'starting' canvas
CMD="$CONVERT_BIN -size ${WIDTH}x$HEIGHT plasma:fractal $BACKGROUNDIMAGE"
if [ $VERBOSE -ne 0 ] ; then echo $CMD; fi
eval $CMD
IMAGETOUSE=$INPUTIMAGE
else
BACKGROUNDIMAGE="$BACKGROUNDIMAGENAME"
WIDTH=`identify -format "%w" "$BACKGROUNDIMAGE"`
HEIGHT=`identify -format "%h" "$BACKGROUNDIMAGE"`
if [ -z "$WIDTH" -o -z "$HEIGHT" ] ; then
failbail "cannot identify size of image $BACKGROUNDIMAGE" ;
fi
INPUTIMAGERESIZED="$TMPDIR/$RESIZEIMAGENAME"
CMD="$CONVERT_BIN \"$INPUTIMAGE\" -auto-orient -resize ${WIDTH}x$HEIGHT \
-background $FILLUPCOLOR -compose Copy -gravity center -extent ${WIDTH}x$HEIGHT \"$INPUTIMAGERESIZED\""
if [ $VERBOSE -ne 0 ] ; then echo $CMD; fi
eval $CMD
IMAGETOUSE=$INPUTIMAGERESIZED
fi
if [ $VERBOSE -ne 0 ] ; then echo "[debug] imagesize: $WIDTH $HEIGHT"; fi
# generate a mask image
if [ $GENERATE_MASK -ne 0 ] ; then
TMPMASKIMAGENAME="$TMPDIR/$MASKIMAGENAME"
generate_voronoi_template "$WIDTH" "$HEIGHT" "$NUMSEGMENTS"
else
TMPMASKIMAGENAME="$MASKIMAGENAME"
fi
# debug: convert labelled mask image to 256 colors
# convert -colors 256 -colorspace GRAY $MASKIMAGENAME ?
# output name
if [ $RESULTIMAGEBASE_GIVEN -ne 0 ]
then
RESULTIMAGEBASE=`basename "$INPUTIMAGE"`
fi
for THRESHOLD in `seq -w 0 1 "$NUMSEGMENTS"`
do
RESULTIMAGE="${RESULTIMAGEBASE}_$THRESHOLD.$OUTPUTFILEFORMAT"
# debug: THRESHOLD=2
# generate black/white image from mask
# this is something to be worked out:
# color values are two bytes, gray value 1/255 is (257, 257, 257) #010101010101, gray value 2/255 ( 514, 514, 514) #020202020202
# $((256*$THRESHOLD+$THRESHOLD)) # overflow at 8, using eval
GRAYTHRESH=`expr 256 \* $THRESHOLD + $THRESHOLD`
GRAYMASKFILE="$TMPDIR/${GRAYMASKDEFAULTFILENAME}"
CMD="$CONVERT_BIN \"$TMPMASKIMAGENAME\" -colors 256 -threshold $GRAYTHRESH \"$GRAYMASKFILE\""
# CMD="convert $MASKIMAGENAME -threshold $THRESHOLD $GRAYMASKFILE"
if [ $VERBOSE -ne 0 ]; then echo $CMD ; fi
eval $CMD
# mask the image
CMD="$COMPOSITE_BIN \"$BACKGROUNDIMAGE\" \"$IMAGETOUSE\" \"$GRAYMASKFILE\" \"$RESULTIMAGE\""
if [ $VERBOSE -ne 0 ]; then echo "$CMD" ; fi
eval $CMD
if [ $VERBOSE -ne 0 ]; then echo "result image written to "$RESULTIMAGE ; fi
done
done
# cleanup
# cleanup and exit messages
dallicleanup