Optimize animated GIF size in command-line

How to compress GIF animations? For example by reducing the color index.

The files are generated using Imagemagick but optimization seems complicated.

For example, the following animation is 864KB at 150x119:

example animation

To be used on a Linux machine.


Use gifsicle (available for Mac OS X, Windows, DOS, Debian, FreeBSD and NetBSD).

gifsicle -i anim.gif -O3 --colors 256 -o anim-opt.gif

This will optimize and reduce to 256 colors, if the animation uses more colors (the example image does).

You can reduce the size by reducing the number of colors:

example image with only 256 colors 512KB, 256 colors

example image with only 128 colors 412KB, 128 colors

example image with only 64 colors 340KB, 64 colors

example image with only 32 colors 240KB, 32 colors

example image with only 16 colors 140KB, 16 colors

Based on another Q&A.


Small addition to @qubodup's answer

Add --lossy flag. This flag varies from 30 to 200, where 30 is good enough.

gifsicle -O3 --colors 256 --lossy=30 -o output.gif input.gif