Linux.com

Feature

Command-line animations using ImageMagick

By Shashank Sharma on July 12, 2005 (8:00:00 AM)

Share    Print    Comments   

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

Basic Animation
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<nobr> <wbr></nobr>.gif if you want to use your animation on a Web site, or use<nobr> <wbr></nobr>.mpeg and<nobr> <wbr></nobr>.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
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-><nobr>c<wbr></nobr> 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.

Shashank Sharma specializes in writing about free and open source software for new users and moderates the Linux.com forum boards. He is the coauthor of Beginning Fedora, published by Apress.

Share    Print    Comments   

Comments

on Command-line animations using ImageMagick

Note: Comments are owned by the poster. We are not responsible for their content.

Too much hassle for a beginner

Posted by: Anonymous Coward on July 13, 2005 04:25 PM
GIMP allows one to do that with far less effort and I´d recommended it for most beginners instead. But its nice to see that there is some command line applications that allows people to do something like that, since it can be really useful in scripts.

One could argue that this is useless, but as anyone on this industry can tell you, you´ll only render your animations into a proper movie file after render several Targas in a directory using your favorite 3D animation package, then loading this bunch of TGAs into a post-processing software and/or a non linear video editor.

So a command line utility that can let you adjust some brightness/contrast/gamma/whatever (like ImageMagick can do ALREADY) with just a command within an automated fashion will save you a lot of time and from having some headaches.<nobr> <wbr></nobr>:-)

#

Re:Too much hassle for a beginner

Posted by: Anonymous Coward on July 13, 2005 08:24 PM
There is nothing better for a beginner than starting with the command line, even if there is a GUI its better for him - as a teaching tool - to start with the command line.

#

Re:Too much hassle for a beginner

Posted by: Anonymous Coward on July 14, 2005 02:08 PM
I´ll beg to differ here. Even being a huge Linux fan and proponent as I am, I would never suggest to someone new to computing to retouch some images using command lines and calculations when the GUI approach is so much easier and intuitive as well.

But for batching processing, you can´t beat a could command line app that can be scripted....<nobr> <wbr></nobr>:-)

#

Gimp + IM = powerful

Posted by: Anonymous Coward on July 14, 2005 12:44 AM
Gimp's Script-Fu (the only way to do multiple files) is terribly difficult to use. Enter IM. Need to flip horizontal, vertical, resize or normalize a whole pile of images? How about mass-conversion from one format to another? That is what the command-line is best at. Complex, single image, serial editing = GUI. Simpler, multiple identical edits = IM. Figure out what you need to do in Gimp, then do it to all images in IM.

#

Re:Gimp + IM = powerful

Posted by: Anonymous Coward on July 14, 2005 02:25 PM
While I agree with pretty much everything else in your comment, I´d argue about GIMP being difficult to use to do some small animations.

Actually, it is much easier/intuitive to understand animations as a stack of layers in GIMP. Since one frame tends to be slightly different than the one above it, you can just duplicate a layer on the stack and then performs the changes that you need.

For example, let´s say that you have a butterfly that must change its wing colors in the course of its flight. In GIMP, you just need to start a new canvas, setup a background layer, draw/import/whatever a butterfly in a invisible layer (alpha channel for those that might have a pet peeve with this description...<nobr> <wbr></nobr>:-)) at any point of the canvas and then duplicate this layer several times, changing the butterfly aspect (for a more believable flight), colors and position. The possibilities are endless since one can use all the filters and effects available to GIMP, like motion blur for starters.

When you´re happy with the results, all you have to do is save the result as a GIF file. GIMP´ll ask you if the layers should be treated as different frames of an animation and even gives a little bit of control about the animation, like loops, duration of frames, etc.

Artists like jimmac and Garrett LeSarge (both of GNOME fame) have been using this technique to create some amazing animated gifs for banners and such for quite sometime. Their websites are "must-see" for anyone interested in using GIMP.

The GIMP website - <a href="http://www.gimp.org/" title="gimp.org">http://www.gimp.org/</a gimp.org> - has several tutorials with a easy language targeted to beginners and advanced users alike and there you´ll be able to spot a handful of them for animation.

Script Fu is another story. But even Script Fu can be leveraged after some practice (which admittedly I lack...<nobr> <wbr></nobr>:-))

#

what is my ip?

Posted by: Anonymous Coward on August 12, 2005 04:11 AM
what is your ip? <a href="http://whatismyip.host.sk/" title="whatismyip.host.sk">what is my ip</a whatismyip.host.sk>

#

ravindra mudumby-explanation

Posted by: Administrator on July 13, 2005 12:18 AM
Use the mogrify program to resize an image, blur, crop, despeckle, dither, draw on, flip, join, re-sample, and much more. This tool is similiar to convert except the original image file is overwritten with any changes you request. See Command Line Processing for advice on how to structure your mogrify command or see below for example usages of the command.
Example Usage

We list a few examples of the mogrify command here to illustrate its usefulness and ease of use. To get started, lets reduce the size of our rose:

mogrify -resize 50% rose.jpg

rose ==> rose

You can resize all your JPEG images in a folder to a maximum dimension of 256x256 with this command:

mogrify -resize 256x256 *.jpg

Finally, we convert all our PNG images to the JPEG format:

mogrify -format jpg *.png

Here image files 1.png, 2.png, etc., are left untouched and files 1.jpg, 2.jpg, etc., are created. They are copies of their respective PNG images except are stored in the JPEG image format.

#

This story has been archived. Comments can no longer be posted.



 
Tableless layout Validate XHTML 1.0 Strict Validate CSS Powered by Xaraya