How to Extract All Frames From a GIF Using ImageMagick?

Extracting individual frames from an animated GIF is a straightforward process when using ImageMagick, a powerful command-line tool for image manipulation. By using the convert command along with specific syntax to handle frame numbering, you can instantly split an animation into its component static images. This guide provides the exact command syntax, breaks down how it works, and offers practical examples for different file formats.

The Basic Syntax

To extract all frames from an animated GIF, you need to provide the input GIF file and specify an output filename that includes a scene number format. This ensures that each frame is saved as a separate, sequentially numbered file.

magick convert input.gif output_%d.png

Note for Modern ImageMagick Users: In ImageMagick v7 and later, the convert command is replaced by the primary magick command, though magick convert still works for backwards compatibility.


Command Breakdown

Understanding the individual components of the command helps you customize the output to fit your specific workflow:


Customizing Your Output

Depending on your project requirements, you can modify the syntax to change how the output files are named or formatted.

Padding Frame Numbers with Leading Zeros

If your GIF has dozens or hundreds of frames, standard numbering (_0, _1, _10) can mess up file sorting in your operating system’s file explorer. You can pad the numbers with leading zeros by using %02d (for two digits) or %03d (for three digits).

magick convert animation.gif frame_%03d.png

This command will output files named frame_000.png, frame_001.png, frame_002.png, and so on.

Coalescing Layered GIFs

Many animated GIFs use optimization techniques where each frame only stores the pixels that change from the previous frame. If you extract these frames directly, you might end up with “ghosted” or partially transparent images. To fix this, use the -coalesce flag to reconstruct the full image for each frame before saving.

magick convert animation.gif -coalesce frame_%d.png

Extracting a Specific Frame Range

If you do not want to extract the entire animation, you can specify a single frame or a range of frames by appending brackets to the input filename.

magick convert animation.gif[0] first_frame.png
magick convert animation.gif[0-5] frame_%d.png