How to Create a GIF from PNGs with ImageMagick?

This article provides a straightforward, step-by-step guide on how to use the ImageMagick convert command to assemble a sequence of static PNG images into an animated GIF. You will learn the exact command-line syntax, how to control the animation speed using delay timings, and how to optimize the final file size for web use.

The Basic Command

To create an animated GIF from a series of PNG files, you need to open your terminal or command prompt, navigate to the folder containing your images, and run the convert command.

The most basic syntax looks like this:

convert *.png animation.gif

This command takes every PNG file in the current directory, sorts them alphabetically by filename, and combines them into a single GIF named animation.gif.

Controlling Animation Speed and Looping

By default, ImageMagick will cycle through the images as fast as possible, and the animation might only play once. To make a smooth, looping animation, you need to add specific flags:

Here is how to combine these flags into a single command:

convert -delay 20 -loop 0 *.png animated_loop.gif

Note: The -delay modifier must be placed before the input images (*.png) so that ImageMagick applies the timing constraint to those files as they are being read.

Optimizing the Output File Size

Animated GIFs can quickly become large files. You can significantly reduce the output size by using ImageMagick’s built-in optimization layers.

The -layers Optimize flag compares consecutive frames and only stores the pixels that actually change from one frame to the next, rather than saving the entire image over again.

convert -delay 20 -loop 0 *.png -layers Optimize optimized_animation.gif

Handling Specific File Sequences

If you do not want to include all PNGs in the folder, or if your files are not named sequentially, you can list the input files individually in the exact order you want them to appear in the animation:

convert -delay 15 frame1.png frame2.png frame3.png output.gif