Skip to content
MagickCmdErrors › Animated GIF turns into garbage after resizing

Animated GIF turns into garbage after resizing

Optimised GIFs store partial frames, so resizing them directly scrambles the animation. Why -coalesce is mandatory, and the correct resize and optimise pipeline.

You resized a working animated GIF and got flickering fragments, ghosting, or frames that look like torn strips of the original.

Why

An optimised GIF does not store complete frames. After the first, most frames contain only the rectangle that changed since the previous one, plus an offset saying where to paste it. That is what makes GIFs small.

When you resize such a file directly, ImageMagick scales those fragments and their offsets independently, and rounding makes them no longer line up. The animation falls apart.

The fix

magick input.gif -coalesce -resize 320x -layers Optimize output.gif

Three parts, all necessary:

  • -coalesce rebuilds every frame as a complete image, so there are no partial rectangles left to misalign.
  • -resize then operates on whole frames.
  • -layers Optimize re-applies the frame optimisation afterwards. Leave it out and the file can be several times larger than the original.

The same rule applies to every GIF operation

magick in.gif -coalesce -crop 400x400+0+0 +repage -layers Optimize out.gif
magick in.gif -coalesce -colorspace Gray -layers Optimize out.gif
magick in.gif -coalesce -rotate 90 -layers Optimize out.gif
magick in.gif -coalesce frame_%03d.png

Any time you modify pixels in an animation, coalesce first.

The -layers options

OptionEffect
-layers CoalesceSame as -coalesce — expand to full frames
-layers OptimizeFull optimisation: frame, transparency and palette
-layers OptimizeFrameOnly reduce each frame to its changed region
-layers OptimizeTransparencyMake unchanged pixels transparent
-layers RemoveDupsDrop duplicate frames, merging their delays

Frame timing gets lost

If your GIF plays at the wrong speed after processing, reset the delay explicitly. Note that -set delay after coalescing applies to all frames:

magick in.gif -coalesce -resize 320x -set delay 8 -layers Optimize out.gif

Check what you started with:

magick identify -format '%f[%s] %wx%h %Tcs\n' in.gif

%T is the delay in hundredths of a second per frame. Variable-delay GIFs lose their timing when you set a single value — if that matters, process frames individually and rebuild.

Disposal methods

If frames leave trails or the background flashes, the disposal method is wrong. It controls what happens to a frame before the next is drawn:

magick in.gif -coalesce -resize 320x -dispose Background \
  -layers Optimize out.gif
  • None — leave the frame in place
  • Background — clear to the background colour
  • Previous — restore what was there before

Coalescing then re-optimising normally sorts this out. Set it manually only if artefacts persist.


Related

Copied