TechEarl

How to Reverse an Animated GIF From the Command Line

Reverse an animated GIF from the command line with ImageMagick or ffmpeg. The -coalesce step is mandatory (GIF frames are diffs), plus a boomerang that plays forward then backward.

Ishan Karunaratne⏱️ 7 min readUpdated
Share thisCopied
Reverse an animated GIF from the command line with ImageMagick or ffmpeg, why -coalesce is required, and how to build a forward-then-reverse boomerang loop.

The shortest way to reverse an animated GIF from the command line is one ImageMagick command:

bash
magick in.gif -coalesce -reverse out.gif

That reads the GIF, rebuilds every frame in full, flips the frame order, and writes a new file that plays backward. The one part you cannot skip is -coalesce, and the rest of this page is mostly about why.

Why -coalesce is not optional

GIF is a delta format. To save space, most encoders store each frame as only the pixels that changed since the previous frame, on top of a disposal method that says what to do with the old frame first. Frame 5 is meaningless without frames 1 through 4 painted underneath it.

Reverse the frame order without rebuilding those frames and you hand the GIF its diffs in the wrong sequence. The result is the classic corrupted reverse: smears, trails, frames that never clear, a background that builds up garbage. The animation is "reversed," but it looks broken.

-coalesce fixes this by flattening every frame into a complete, standalone image first (the full canvas as it would actually appear at that moment). Once each frame is whole, reversing the order is safe. So the rule is simple: coalesce, then reverse. Always in that order.

bash
# Wrong: reverses the diffs, corrupts the animation
magick in.gif -reverse out.gif

# Right: full frames first, then flip the order
magick in.gif -coalesce -reverse out.gif

After coalescing, every frame is a full image, which makes the file larger. Re-optimize on the way out and set the loop:

bash
magick in.gif -coalesce -reverse -layers OptimizePlus -loop 0 out.gif

-layers OptimizePlus re-derives the inter-frame diffs (and frame disposal) so the output is small again, and -loop 0 means loop forever. On ImageMagick 6 the command is convert instead of magick; the flags are identical. Everything here works the same on a modern ImageMagick 7 install.

Reversing with ffmpeg instead

If ffmpeg is already your tool for video, its reverse filter handles GIFs too. The naive form works:

bash
ffmpeg -i in.gif -vf reverse out.gif

But ffmpeg's default GIF encoder builds a single global palette and dithers hard against it, so a colorful source comes out muddy. Run the frames through palettegen and paletteuse to keep the quality:

bash
ffmpeg -i in.gif -vf "reverse,split[s0][s1];[s0]palettegen[p];[s1][p]paletteuse" out.gif

That reverses the frames, generates an optimal 256-color palette from the reversed stream, and remaps the frames to it. One caveat worth knowing: ffmpeg's docs note that the reverse filter "requires memory to buffer the entire clip, so trimming is suggested." It holds the whole clip in RAM before it can emit the last frame first, so it is fine for a short GIF and a bad idea on a long one. For reversing actual video files (with the audio reversed too via areverse), see reverse a video with ffmpeg.

Reversing with gifsicle

gifsicle has no --reverse flag, but it does not need one: its frame-selection syntax can pull frames in any order, including backward. A descending frame range reverses the animation in a single pass:

bash
gifsicle in.gif "#-1-0" -o out.gif

The #-1-0 selects frames from the last (#-1) down to the first (#0), so the output plays backward. Quote the range in your shell so # is not read as a comment. gifsicle works on the GIF's own frames directly rather than coalescing to a full canvas the way ImageMagick does, so for a heavily delta-optimized source you may still want to round-trip through ImageMagick's -coalesce -reverse -layers OptimizePlus if a backward play exposes disposal artifacts. For a simple GIF, the one-line gifsicle form is the lightest option.

Make a boomerang (forward then reverse)

A boomerang plays the clip forward, then immediately backward, so it loops seamlessly with no jump cut. You build it by concatenating the GIF with its own reverse. ffmpeg does this in one pass:

bash
ffmpeg -i in.gif -filter_complex "[0]reverse[r];[0][r]concat,split[s0][s1];[s0]palettegen[p];[s1][p]paletteuse" boomerang.gif

Reading the filter graph: [0]reverse[r] makes a reversed copy, [0][r]concat glues the original in front of the reversed copy, and the split/palettegen/paletteuse tail does the same quality-preserving palette pass as before.

ImageMagick gets there a different way, by appending the coalesced frames to a reversed copy of themselves with a clone:

bash
magick in.gif -coalesce \( -clone -1-0 \) -layers OptimizePlus -loop 0 boomerang.gif

( -clone -1-0 ) clones the existing frames in reverse order (last index to first) and appends them, so you end up with forward frames followed by backward frames in a single image list. Trim the duplicated turnaround frame if the pause at the ends bothers you (drop the first or last index from the clone range).

Changing the loop or the speed while you are at it

Reversing is often half of what people actually want. If the reversed GIF should play faster, slower, or loop a set number of times, that is a separate adjustment to the frame delay or loop count, covered in change an animated GIF's speed from the command line. For every GIF and video flag in one place, the ffmpeg command cheat sheet is the reference.

FAQ

See also

Sources

Authoritative references this article was fact-checked against.

TagsImageMagickffmpeganimated gifreverse gifboomerang gifCLIgifsicle

Found this useful? Pass it on.

Copied

Ishan Karunaratne

Tech Architect · Software Engineer · AI/DevOps

Tech architect and software engineer with 20+ years building software, Linux systems, and DevOps infrastructure, and lately working AI into the stack. Currently Chief Technology Officer at a healthcare tech startup, which is where most of these field notes come from.

Keep reading

Related posts

How to Convert a PSD to PNG From the Command Line

Convert a PSD to PNG from the command line with ImageMagick. The one trick that matters: design.psd[0] selects the flattened composite, so you get one PNG instead of a folder full of separate layers.