The fastest way to compress an animated GIF from the command line is gifsicle, and the one command that does most of the work is this:
gifsicle -O3 --lossy=80 --colors 128 in.gif -o out.gifThat stacks three levers: maximum optimization (-O3), lossy compression (--lossy=80), and a smaller palette (--colors 128). On a typical screen-recording GIF that routinely cuts the file in half or more, with damage you have to squint to see. Most tutorials show only -O3, which is why people walk away disappointed their GIF "only got 20% smaller." The real win is --lossy, and almost nobody mentions it.
Here is the breakdown, plus the honest part at the end: if you control how the GIF is made, the bigger win usually is not optimizing the GIF at all.
Install gifsicle
# macOS
brew install gifsicle
# Debian / Ubuntu
sudo apt install gifsicle
# Fedora
sudo dnf install gifsicleThe --lossy flag merged into mainline gifsicle in 1.92 (April 2019); before that it lived in Kornel Lipiński's separate "giflossy" fork. Any version your package manager ships today has it. If a --lossy command errors with "unknown option," check gifsicle --version; an ancient build is the only reason it would.
-O3: lossless optimization first
The -O flag reorganizes the GIF so unchanged pixels between frames are not re-stored. It is lossless and never touches image quality:
gifsicle -O3 in.gif -o out.gif-O3 is the most aggressive level: it tries several optimization methods (slower, and the manual is honest that it only "sometimes" beats the lower levels). -O1, which stores only the changed portion of each image, is the default; -O2 adds transparency on top of that. On its own -O3 is modest, often a 10 to 25 percent reduction, which is exactly where the old "gifsicle optimization" guides stopped. It is the floor, not the ceiling.
--lossy: the flag that actually shrinks the file
This is the gem. --lossy lets gifsicle introduce small color artifacts to find longer compressible runs, the same idea JPEG uses for stills:
gifsicle -O3 --lossy=80 in.gif -o out.gifThe number is how much loss to allow. A bare --lossy defaults to 20; higher means smaller files and more visible noise. I find --lossy=80 is the sweet spot for screen recordings and UI demos. For clean line art or text, drop to --lossy=30 or so, because flat color areas show banding sooner. Try a couple of values and eyeball the output; the right number depends on the content.
--colors: shrink the palette
A GIF stores at most 256 colors. Cutting that ceiling drops the per-frame data and stacks with everything above:
gifsicle -O3 --lossy=80 --colors 128 in.gif -o out.gif--colors takes a value between 2 and 256. Screen recordings and flat-design demos rarely need the full 256; 128 or even 64 is usually indistinguishable. Photographic or gradient-heavy GIFs are the exception. Add --dither if banding appears and you want to trade a little file size to hide it.
Resize: the biggest lever of all
Pixel count is the single largest factor in GIF size. Halving the dimensions roughly quarters the data. gifsicle keeps the animation intact while resizing, which is the trap with ImageMagick (a naive convert -resize on a GIF can flatten it to the first frame):
# Constrain width to 480px, height auto to keep aspect ratio
gifsicle --resize-fit-width 480 in.gif -o out-480.gif
# Or scale by a factor
gifsicle --scale 0.5 in.gif -o out-half.gif--resize-fit-width (and --resize-fit-height) only shrink if the GIF is larger than the target, so they never upscale. --scale 0.5 halves both dimensions. If a 1200px-wide GIF goes into a post that renders it at 600px, resizing to 600 first is free quality and a huge size cut.
Drop frames or slow the playback
If the motion can tolerate it, throwing away every other frame nearly halves the size. There is no single "drop frames" flag; you select the frames to keep and bump the delay to hold the same duration:
# Keep every 2nd frame (0-indexed) and double the delay to 10 (in 1/100s)
gifsicle in.gif $(seq 0 2 60 | sed 's/^/#/') --delay 10 -o out.gif--delay sets the inter-frame delay in hundredths of a second, so --delay 10 is 10 frames per second. This is a blunt tool and it shows on fast motion, but for a slow UI walkthrough it is an easy extra cut.
Batch a whole directory
--batch edits files in place, so you can optimize a folder of GIFs in one pass:
gifsicle --batch -O3 --lossy=80 --colors 128 *.gif--batch rewrites each input file rather than writing to a single -o target. Make a copy first if you want the originals.
The honest truth: maybe do not ship a GIF at all
Optimizing a GIF is treating the symptom. The format is from 1987 and is genuinely bad at compressing video; even a heavily optimized GIF is enormous next to a real codec. Two things beat post-optimization outright:
- Generate the GIF well in the first place. If you are converting a video, a quality GIF comes from a per-clip palette, not from squeezing a bad GIF afterward. See convert a video to a GIF with ffmpeg for the two-pass
palettegen/paletteuseflow that produces a smaller, cleaner GIF than any amount of gifsicle on a careless export. - For the web, use a video, not a GIF. A muted, looping, autoplaying MP4 or WebM in a video tag is commonly 5 to 10 times smaller than the equivalent GIF at better quality. GIF only earns its keep where the destination genuinely cannot play video: some chat apps, email, and older platforms that only accept GIF.
So the decision tree is simple. Shipping to a web page you control, encode a video. Stuck with GIF as the delivery format, generate it from a good palette and then run gifsicle -O3 --lossy=80 --colors 128 to claw back the rest.
gifsicle vs ImageMagick for animated GIFs
For static images ImageMagick is the everything-tool, but for animated GIFs gifsicle is the specialist and it wins. ImageMagick can optimize and resize GIFs, but it is easy to accidentally collapse the animation to a single frame, and its lossy story is weaker. gifsicle was built for exactly this job. If you also need to compress stills, see convert an image to WebP from the command line; for the broader media toolkit, the ffmpeg commands cheat sheet is the reference.
FAQ
See also
- Convert a video to a GIF with ffmpeg: the two-pass palette flow that makes a small, clean GIF in the first place.
- Convert an image to WebP from the command line: the modern format for stills, far smaller than PNG or JPEG.
- ffmpeg commands cheat sheet: the broader media toolkit for video, audio, and image work in one reference.
Sources
Authoritative references this article was fact-checked against.





