-
Notifications
You must be signed in to change notification settings - Fork 1
/
mkanimgif
163 lines (130 loc) · 3.4 KB
/
mkanimgif
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
#!/bin/ksh
usage () {
echo "mkanimgif usage: mkanimgif [ options ] IMAGES"
echo "The IMAGES can be GIFs or PGM/PPM files. Other files might work."
echo
echo "Options:"
echo " -I use ImageMagick"
echo " -G use Gifsicle"
echo " -l do not loop video (default loops forever)"
echo " -D use dithering"
echo " -q N set quant to N (default is 255)"
echo " -d N set delay to N (default is 5)"
echo " -o FILE save output to FILE (default is 'animation.gif')"
echo " -r WxH resize output to Width x Height (no default resize)"
echo
echo "This is just a friendly wrapper for gifsicle, that tool can do much,"
echo "much more, including exact loop count, variable delays, interlacing..."
echo "http://www.lcdf.org/gifsicle/"
exit
}
quant=255
gifloop=-l
imgloop="-loop 0"
gifdelay="-d"
imgdelay="-delay"
gifdither=
imgdither=
use=gifsicle
delay=5
out="animation.gif"
resize=""
del=
imgs=
argloop=yes
while [ X$argloop != X ] ; do
case "$1" in
# -G use Gifsicle
-G) shift; use=gifsicle ;;
# -I use ImageMagick
-I) shift; use=convert ;;
# -D use dithering
-D) shift; imgdither="-ordered-dither o8x8,8"; gifdither="-f"; ;;
# -l do not loop video
-l) shift; imgloop=""; gifloop=""; ;;
# -d N set delay to N (try 1 to 5)
-d) shift; delay="$1"; shift ;;
# -q N set quant to N (try 1 to 5)
-q) shift; quant="$1"; shift ;;
# -o FILE save output to FILE
-o) shift; out="$1"; shift ;;
# -r WxH resize output to Width x Height
-r) shift; size="$1" ; resize="-resize $1"; shift ;;
--help) usage ;;
-*) echo "$1 unrecognized option"; usage ;;
'') if [ "X$imgs" = X ] ; then
echo "Insufficent arguments"; usage
else
argloop=
fi
;;
*) imgs="$imgs $1" ; lastimg="$1" ; shift ;;
esac
done
prepared=n
pnm=n
case "$use" in
convert) prepared=y ;;
gifsicle)
case "$lastimg" in
*.[gG][iI][fF]) prepared=y ;;
*.[pP][bBgGpPnNaA][mM]) pnm=y ;;
esac
;;
esac
if [ $prepared = n ] ; then
pre=/tmp/$$
if [ $pnm = y ] ; then
echo "Selecting colors"
pnmcat -tb $imgs | pnmcolormap $quant > $pre.map
colors="-map $pre.map"
quant=""
else
colors=""
quant="| ppmquant $quant 2>/dev/null"
fi
case "$size" in
*x*) xysize=$(echo $size | tr 'x' ' ')
resizetool="pnmscale -xy $xysize"
;;
*) resizetool=cat
;;
esac
if [ $pnm = n ] ; then
if [ $resizetool = cat ] ; then
resizetool=anytopnm
else
resizetool="anytopnm | $resizetool"
fi
fi
echo "Making frames"
gifs=
n=1
for img in $imgs ; do
echo "< $img $resizetool $quant | ppmtogif $colors > $pre.$n.gif"
eval "< $img $resizetool $quant | ppmtogif $colors > $pre.$n.gif"
gifs="$gifs $pre.$n.gif"
n=$(( $n + 1 ))
done
del="$gifs"
imgs="$gifs"
fi
case "$use" in
convert)
echo "Running: $use $resize $imgdelay $delay $imgloop ( images ) \\"
echo " $imgdither -coalesce -layers OptimizeTransparency $out"
time convert $resize $imgdelay $delay $imgloop \
$imgs \
$imgdither -coalesce \
-layers OptimizeTransparency \
"$out"
;;
gifsicle)
echo "Running: $use -O3 -o $out $gifdither $gifdelay $delay $gifloop ( images )"
gifsicle -O3 -o $out $gifdither $gifdelay $delay $gifloop $imgs
;;
esac
if [ $prepared = n ] ; then
rm -f $pre.map $del
fi
ls -lh $out