-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpic2gif.sh
51 lines (46 loc) · 1.46 KB
/
pic2gif.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
#!/bin/bash
#-----------------------------------------------------------
# pic2gif <https://svmgrg.github.io/>
#-----------------------------------------------------------
# pic2gif is a toy program that creates a GIF of an input
# image with increasing and descreasing threshold values
# (requires 'convert' function of ImageMagick).
#-----------------------------------------------------------
# Running the program:
# For black and white gif:
# $ bash pic2gif.sh <input_image>
# For colored gif:
# $ bash pic2gif.sh <input_image> -c
#-----------------------------------------------------------
# Output is an image of the same name with extension as .gif
#-----------------------------------------------------------
# reduce the dimensions of the image to less than 400x400
WIDTH=$(identify -format '%w' $1)
HEIGHT=$(identify -format '%h' $1)
if [ $WIDTH -gt 400 ] || [ $HEIGHT -gt 400 ]; then
convert -resize 400x400 $1 .tmp.png
else
convert $1 .tmp.png
fi
# forward loop thresholding
for COUNTER in {000..099}
do
if [ $# -eq 2 ] && [ $2 == '-c' ]; then
convert .tmp.png -separate -threshold $COUNTER% -combine .temp_$COUNTER.png
else
convert .tmp.png -threshold $COUNTER% .temp_$COUNTER.png
fi
done
# backward loop thresholding
COUNTER=100
for PREV in {099..000}
do
cp .temp_$PREV.png .temp_$COUNTER.png
let COUNTER+=1
done
# create the gif
OUTPUT=${1%.*}
convert -delay 5 .temp_*.png $OUTPUT.gif
# remove temporary images
rm .temp_*.png
rm .tmp.png