July 12, 2005

Command-line animations using ImageMagick

Author: Shashank Sharma

If the success of the "Shrek," "Toy Story," "Stuart Little," "The Incredibles," and many other Hollywood hits is any indication, animations add glitz to the mundane. While animation in the movies still requires professional animation packages like Blender, you can make simple animations using the command-line wizardry of ImageMagick.

There are three basic steps to making an animation. First you need an idea. Next, you need a set of images to run through in individual frames. You can get the images from your digital camera or your scanner or draw them using ImageMagick or the GIMP. Once you have these ready, all you need to do is to put them together into an animation.

Your first animation

A basic animation could just run through several images. You can do this easily using the animate command:

animate image1.gif image2.gif image3.gif

A basic animation

IM displays the images in the order they are listed. Since animate does not save animations, its use is limited to testing. This animation will quickly blur through all the three GIF files.

If you need to pause on an image before moving to the next one, use the -delay switch. It sets the time pause between images in units of 100th of a second:

animate -delay 200 frame1.gif frame2.gif frame3.gif

This sequence of images will spend two seconds on each image. But this command runs the animation just once. It's left to the -loop switch to determine the number of times an animation is to cycle through the frames:

animate -delay 300 frame1.gif frame2.gif frame3.gif -loop 3

This loops the animation three times. -loop 0 loops the animation infinitely.

Placing images

To save an animation we use the convert command, supplying a filename with an appropriate extension. Use.gif if you want to use your animation on a Web site, or use.mpeg and.avi if you want these to play on a media player.

convert -delay 30 frame1.gif frame2.gif frame3.gif \
> -loop 0 playme.gif

This results in an animation, playme.gif, made up of three images looping infinitely.

The animations we have creating so far adjust their size depending upon the size of the constituent images. This can cause havoc if you use them within Web pages. We need to specify a canvas on which to lay out our animation and its constituent images. Let's use a big canvas and place the same image onto it at different places:


convert -delay 0 -size 100x100 xc:green \
> -delay 50 -page +32+62 ball.gif -page +2+35 ball.gif \
> -delay 10 -page +31+2 ball.gif -page +62+32 ball.gif \
> -loop 0 anim.gif

The -page switch places the images at the specified locations. We have also used -delay with every image to set its own pause time.

You can also use the -pause switch to supply the time delay between animation loops:


convert -delay 0 -size 100x100 xc:green \
> -delay 50 -page +32+62 ball.gif -page +2+35 ball.gif \
> -delay 10 -page +31+2 ball.gif -page +62+32 ball.gif \
> -loop 0 -pause 3 anim.gif

The anim.gif image is a green canvas of 100x100 pixels, within which the same image is displayed at multiple locations, each for a specified time, and the sequence repeats after three seconds.

Note that in this animation, new images appear alongside other images. If you want the previous image to disappear before the new one arrives on a clean canvas, use the -dispose switch. It controls the way an image should to be treated after being displayed:


convert -dispose none -delay 0 base.gif \
> -dispose previous -delay 100 \
> -page +32+62 ball.gif -page +2+35 ball.gif \
> -page +31+2 ball.gif -page +62+32 ball.gif \
> -loop 0 animation.gif

An animation made with the dispose previous switch

The most important option of dispose is previous. In an animation, the images before dispose previous become the base image, and the animation returns to this base image after each image is displayed. The dispose in the base image in the example above is thus set to zero. So, base.gif becomes the base image, and the animation returns to it after displaying each image in the sequence. So, the effective order of the animation is: base.gif->big.gif->base.gif->ball.gif->base.gif->c ircle.gif, and so on.

Using convert you can also combine two animations into one file:

convert animation.gif anim.gif combined_anim.gif

This combines the first two GIF files into the last animation in the listed sequence.

Conclusion

ImageMagick is a robust command-line utility capable of doing simple image manipulations like cropping images and changing image formats to drawing various shapes, mosaics, and 3D images to creating animations.

You can create some interesting animations using the commands in this article. You could animate trees swaying in the winds, create ripples in a pond, show cloud movement through the sky, or lip-sync yourself to "Hasta la vista, baby."

Shashank Sharma is a computer science student who loves to write about free and open source software for new users.

Click Here!