66

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.

qubodup
  • 8,365
  • 7
  • 36
  • 49
  • See also [Is there something like pngcrush for GIFs?](https://stackoverflow.com/q/6911972/3075942) on Stack Overflow for more answers. – user May 06 '19 at 09:36
  • This Q&A has `ffmpeg` answers https://superuser.com/a/1695537/1076303 – M.Viking May 27 '22 at 21:14

2 Answers2

81

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.

qubodup
  • 8,365
  • 7
  • 36
  • 49
  • 3
    Thanks for your answer. It helped me a lot. I got the best result using `gifsicle -i anim.gif --optimize=3 -o anim-opt.gif` – Mostafa Ahangarha Nov 21 '17 at 12:50
  • works great! for some odd reason, im using a shell script that uses imagemagick and oddly it isnt working with gifs we got from videos via final cut pro (exported as image sequence) then exported thru photoshop but otherwise works on other gifs. – SMT Nov 30 '17 at 18:56
  • Available in FreeBSD ports as well: https://www.freshports.org/graphics/gifsicle/ – Mateusz Piotrowski Sep 16 '18 at 15:55
  • Could you explain what the -03 means? – Sam Dec 19 '19 at 04:41
  • @Jacob "-O[level] - Optimize output GIF animations for space. Level determines how much optimization is done; higher levels take longer, but may have better results. There are currently three levels: […] -O3 - Try several optimization methods (usually slower, sometimes better results)." From the gifsicle man-page – Jonta Mar 31 '20 at 13:37
  • reduced the size up to 75% for me when setting the colors to 16 for me :) – Mona Jalal Sep 27 '20 at 00:47
  • Using `--scale=0.25` together with `--colors 128` has reduced my files to 1/12 of their original size: `gifsicle -O3 --colors 128 --scale=0.25 -o output.gif input.gif` – DotBot Dec 01 '21 at 18:17
  • Why did you turn interlacing on with `-i`? What advantage or effect does it have? – Gabriel Staples Jan 10 '22 at 17:54
  • Interesting. It tells me some things about how "useful" my options are. I ran `time gifsicle in.gif -i -O3 --colors 256 > out256.gif` and it told me the following info: 1) `gifsicle: warning: huge GIF, conserving memory (processing may take a while)`, 2) (that `--colors 256` didn't help me much) `gifsicle: warning: trivial adaptive palette (only 196 colors in source)`, 3) (that `-i` was useless in this case) `gifsicle: warning: useless interlace-related frame option (It didn’t affect any frame.)` – Gabriel Staples Jan 10 '22 at 18:06
  • I just want to point out how important the `-O3` part is **each time** you use the `--colors` option! I thought that if I ran `gifsicle in.gif -O3 > out.gif` once, then I could subsequently skip the `-O3` part to speed things up and just use `gifsicle in.gif --colors 128 > out2.gif`, `gifsicle in.gif --colors 64 > out3.gif`, etc, for subsequent calls. This is NOT the case. One should use `-O3` _every time_! The `--colors 8` option, for instance, on the pre-optimized file as input is _2~3x larger_ than using `-O3 --colors 8` on the same already-optimized input file. – Gabriel Staples Jan 10 '22 at 20:24
12

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
Levon
  • 506
  • 5
  • 5
  • The version of `gifsicle` that comes with Ubuntu 18.04 does not have the `--lossy` feature. If I look at the bottom of `man gifsicle` I see it is version 1.91 from 11 July 2017. The version that comes with Ubuntu 20.04, however, _does_ have the `--lossy` feature. The bottom of its man page shows it is version 1.92 from 11 July 2017. `gifsicle --version` on Ubuntu 18.04 shows `LCDF Gifsicle 1.91 Copyright (C) 1997-2018 Eddie Kohler`. From Ubuntu 20.04 it shows `LCDF Gifsicle 1.92 Copyright (C) 1997-2019`. Again, **version 1.91 does _not_ have the `--lossy` feature, but version 1.92 _does_.** – Gabriel Staples Jan 11 '22 at 15:21
  • 1
    The [official changelog `NEWS.md` file here](https://github.com/kohler/gifsicle/blob/master/NEWS.md) confirms that `--lossy` was added with Version 1.92. – Gabriel Staples Jan 11 '22 at 17:32
  • 1
    I ran a bunch of tests, and for my gif, saw no improvement when using `-O3 --lossy`. Rather than decreasing file size, filze size actually _increased_ by a tiny amount of about 0.3%. Furthermore, the file sizes were _exactly identical_ for me for both `-O3 --lossy=30 --colors 16` and `-O3 --lossy=100 --colors 16`, as well as for `-O3 --lossy=30 --colors 256` and `-O3 --lossy=100 --colors 256`. In other words, whether lossy was set to `30` or `100` seemed to have no difference in my cases whatsoever, for the file I tested at least. – Gabriel Staples Jan 11 '22 at 17:35