Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

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?

share|improve this question
1  
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. –  J. M. Jun 8 '12 at 9:48
2  
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. –  Ajasja Jun 8 '12 at 11:35
2  
You could try something like Export["image.gif", ColorQuantize[#, 16] & /@ imglist]. –  Heike Jun 8 '12 at 14:47
2  
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 –  Jens Jun 9 '12 at 5:13
1  
@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... –  Jeff Atwood Jun 9 '12 at 11:44
show 1 more comment

1 Answer 1

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"]
(*
114000 
57095
*)
share|improve this answer
    
Here's how to verify that only sixteen colors were used: ExportString[ColorQuantize[test, 16], "GIF"]; Length[ImportString[%, "ColorMap"]] –  J. M. Jun 9 '12 at 9:34
    
@J.M. Not sure, but perhaps one should check the "GlobalColorMap" too –  belisarius Jun 9 '12 at 21:58
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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