Trix & Graphix

Simple animations with Imagemagick

Imagemagick allows to generate simple animations (this is, a gif file) by using several input images as frames in a movie. Here I'm going to show the main idea, which it's actually very simple.

In order to have several images to be mounted in the movie, I have used a bash script together with gnuplot. This will generate 20 slightly different images of a sinusoidal fuction, little bit shifted between them. This is the code:



# Note the -w option in seq
for i in $(seq -w 1 $N); do
gnuplot << TOEND
# Setting the output
set terminal postscript eps color "Helvetica" 20
set output 'sin$i.eps'

# This removes the numbers in the graph and sets the grid
set format x ""
set format y ""
set grid

# The plot itself
plot sin(x+2*pi/$N*$i) w l lt 1 lc 3 lw 4 t ''


# Now transform the postscript file into a png one, and remove the eps
convert -density 50 -layers flatten sin$i.eps sin$i.png
rm sin$i.eps

It's important to note the "-w" option in the seq command. This will generate numbers with the correct padding. This is, it will generate the list "01 02 ... 09 10 11 ..." instead of "1 2 ... 9 10 11 ..." which may be a problem when you want to list the resulting files in the correct order to create the movie.

So now we have 20 png files. To merge them, the convert command may be invoked with just an option to set the delay (in miliseconds) between images.

convert -delay 10 sin*png animation.gif

That's all, it's pretty easi, isn't?

Ant this is the result:

EDIT: By some unknown reason, gif animations doesn't seem to work when I upload them to Picasa. Well, you have to trust me, it evolves with time, and it's even pretty funny ;-).

ACTUALIZATION: Using other (my own) web server, I have been abled to upload a real gif animation. With minor changes in the script, this is what I get: