3
$\begingroup$

Related: How to export a Graphics to PNG with BitDepth greater than 8?

I can't find a way to set the BitDepth of an exported GIF. It doesn't need to have great colour data, but I don't want to reduce the resolution. It's going in a powerpoint slide, so I'm trying to reduce its size. With a list of images called imgList, here's what I try:

Export["anim.gif",imgList,"BitDepth"->5, "DisplayDurations"->0.04];

However, no matter what I do, this always exports with a BitDepth of 8. I check this like so:

Import["anim.gif","BitDepth"]

Minimum working example:

imgList = Table[Graphics@Circle[],{i,1,50}];
Export["anim.gif",imgList,"BitDepth"->5, "DisplayDurations"->0.04];
Import["anim.gif","BitDepth"]

Output: 8

Investigating nikie's comment, it appears as though the GIF standard uses a 256-colour palette, and I'm not sure this can be changed. However, I did find this partial solution:

When exporting an (animated) GIF, is it possible to specify the size of the color palette?

Although it doesn't change the actual bit depth of the GIF (I guess this isn't possible as 8 bits is built into the standard?), it does compress the colours of the source images so that they only pull from a limited colour palette. I exported the GIF using this:

imgList = Table[Graphics@Circle[],{i,1,50}];
Export["anim.gif",ColorQuantize[#,2^5]&/@imgList,"DisplayDurations"->0.04];
Import["anim.gif","BitDepth"]

Output: 8

Here, 2^5 indicates that each image should be compressed to pull from a palette of 2^5 colours which corresponds to a bit depth of 5. Although the bit depth of the exported GIF is still 8, the file size is reduced. I guess this is due to smart compression within the GIF standard.

But there's a problem. The colouring of the GIF is unacceptably choppy, even when quantising to 8 bits. There are parts of the image that never change colour, but the GIF renders them as flashing quickly from grey to red. Here's an example still from an 8-bit export using this method:

enter image description here


Update

Here are comparisons of the output when the images are ColorQuantize[#,2^8]-ed...

enter image description here

...and are not: enter image description here

Both of these methods quantise the colours to 8-bits, and both use dithering. Yet one of them is higher quality than the other (i.e. doesn't produce the weird greyness as illustrated above nearly as badly). I want this superior output, at a lower bit depth. How can I achieve this?

Using ColorQuantize with no dithering as nikie suggests does fix the grey artefact problem, but makes the image on the right look much worse:

enter image description here

Is there perhaps a way to fine-tune the dithering to resolve this?

$\endgroup$
  • $\begingroup$ I think the GIF format only supports up to 8 bits per pixel. At least that's what it says on Wikipedia $\endgroup$ – Niki Estner Dec 2 '16 at 14:02
  • $\begingroup$ @nikie - Good thing I'm aiming for less than 8 then. $\endgroup$ – Myridium Dec 2 '16 at 14:02
  • $\begingroup$ Sounds like the dithering messes up your animation. Did you try turning dithering off? i.e. Using ColorQuantize[#,2^5,Dithering -> False] $\endgroup$ – Niki Estner Dec 2 '16 at 15:05
  • $\begingroup$ @nikie - Nice suggestion! I switched OSs and won't be able to try this for a while. I suspect it will solve the problem, but unfortunately there's another half of the image (I've only shown the left half) for which dithering is desired. I think I'll have to find a way to break the image apart, quantize each half with dithering on/off, stitch them back together, and create a GIF. Might be too much trouble. $\endgroup$ – Myridium Dec 2 '16 at 15:10
  • $\begingroup$ @nikie - Please see my update if you have anything you can add. $\endgroup$ – Myridium Dec 3 '16 at 17:49

Your Answer

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

Browse other questions tagged or ask your own question.