I've exported an animation to animated GIF, which generated a 15mb file. I've been asked if I could regenerate the file, limiting it to only 16 colors, but I can't find any documentation on how I might do that. Is there a way to make Export[] reduce the color palette, preferably dithering as needed?

  • 1
    $\begingroup$ Export[] in old versions of Mathematica supported a "ColorReductionPalette" option for exporting GIFs; it seems they have either removed or hidden this functionality. As for dithering, it seems Floyd-Steinberg dithering is already performed by default when exporting GIFs. $\endgroup$ – J. M.'s torpor Jun 8 '12 at 9:48
  • 3
    $\begingroup$ You can always post-process your image using gifsicle or Nconvert or optiview.com. All of which are cross platform alternatives. Since the first two are command-line tools, you can also automate that process from MMA if necessary. $\endgroup$ – Ajasja Jun 8 '12 at 11:35
  • 2
    $\begingroup$ You could try something like Export["image.gif", ColorQuantize[#, 16] & /@ imglist]. $\endgroup$ – Heike Jun 8 '12 at 14:47
  • 3
    $\begingroup$ Using gifsicle as @Ajasja suggested, one can get roughly 30% smaller files while keeping all 256 colors. The command that I used to test this is gifsicle --colors=256 --optimize=2 < in.gif > out.gif $\endgroup$ – Jens Jun 9 '12 at 5:13
  • 1
    $\begingroup$ @jens great tip; gifsicle --optimize=3 --colors=16 < enormous.gif > smaller.gif produces a file half the size of the original 15 megabyte animated GIF file in question. The source animation uses very few colors anyway so 16 colors seems sufficient to my eye... $\endgroup$ – Jeff Atwood Jun 9 '12 at 11:44

Just too long for a comment. Here you can see the savings obtained by using the method explained in Heike's comment above:

i := ArrayPlot[RandomReal[1, {100, 200}], ColorFunction -> Hue];
StringLength@ExportString[k = {i, i}, "GIF"]
StringLength@ExportString[ColorQuantize[#, 16] & /@ k, "GIF"]
  • $\begingroup$ Here's how to verify that only sixteen colors were used: ExportString[ColorQuantize[test, 16], "GIF"]; Length[ImportString[%, "ColorMap"]] $\endgroup$ – J. M.'s torpor Jun 9 '12 at 9:34
  • $\begingroup$ @J.M. Not sure, but perhaps one should check the "GlobalColorMap" too $\endgroup$ – Dr. belisarius Jun 9 '12 at 21:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.