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:
-coalescerebuilds every frame as a complete image, so there are no partial rectangles left to misalign.-resizethen operates on whole frames.-layers Optimizere-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
| Option | Effect |
|---|---|
-layers Coalesce | Same as -coalesce — expand to full frames |
-layers Optimize | Full optimisation: frame, transparency and palette |
-layers OptimizeFrame | Only reduce each frame to its changed region |
-layers OptimizeTransparency | Make unchanged pixels transparent |
-layers RemoveDups | Drop 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 placeBackground— clear to the background colourPrevious— restore what was there before
Coalescing then re-optimising normally sorts this out. Set it manually only if artefacts persist.