Page 1 of 1

Shrinking file size on an animated gif

Posted: 2016-02-24T15:38:51-07:00
by pricj004
Hi,

Version 6.9.3-0 Q16 x86_64 2016-02-19

I have this animated gif: https://cdn-images-1.medium.com/max/160 ... BoCzjA.gif

It's 4.1MB and 800 x 600.

I'm trying to resize it to 700 x 525 (same aspect ratio), using this command:

Code: Select all

convert input.gif -coalesce -resize 700x525 -layers Optimize output.gif
It works, but the output file size is 15MB.

I would have thought, given the dimensions are smaller, the file size would end up smaller?

I have tried the following:

Code: Select all

convert input.gif -coalesce -background White -alpha remove -resize 700x525 -layers Optimize output.gif
convert input.gif -coalesce -bordercolor White -border 0 -resize 700x525 -layers Optimize output.gif
convert input.gif -coalesce -colors 32 -resize 700x525 -layers Optimize output.gif
convert input.gif -coalesce -depth 8 -resize 700x525 -layers Optimize output.gif
convert input.gif -coalesce -quantize lab -resize 700x525 -layers Optimize output.gif
But if anything, these same to make the file larger?

Any ideas? I basically just want the file size to be less than 4.1MB.

Re: Shrinking file size on an animated gif

Posted: 2016-02-24T15:48:10-07:00
by fmw42
When your resize an image, you introduce many new colors. See http://www.imagemagick.org/Usage/anim_o ... or_problem

Thus you would likely need to make one common color map image and use that to reduce the colors of each frame before combining again. See http://www.imagemagick.org/Usage/quantize/#remap but use +dither or -dither none. You could use one frame as the color map image to provide the colors that all the other frames would be map to.

See also http://www.imagemagick.org/Usage/anim_opt/#colortables

Re: Shrinking file size on an animated gif

Posted: 2016-02-24T17:17:09-07:00
by pricj004
Thanks for the advice.

I read the links, and after a bit of trial and error, found that adding -fuzz had the largest effect on file size.

Reducing to a single colour map using +map helped a bit too. I've also added +dither, so my final command at the moment is:

Code: Select all

convert input.gif -coalesce -resize 700x525 -fuzz 2% +dither -layers Optimize +map output.gif
At 4.5MB, it's still not where I want it, but much better.

Re: Shrinking file size on an animated gif

Posted: 2016-02-24T17:35:04-07:00
by snibgo
"-scale" instead of "-resize" will reduce the size.

Re: Shrinking file size on an animated gif

Posted: 2016-02-24T17:47:35-07:00
by pricj004
Huh. So it does. Down to 3.8MB now. Thanks.

Re: Shrinking file size on an animated gif

Posted: 2016-02-24T17:49:32-07:00
by fmw42

Code: Select all

Reducing to a single colour map using +map helped a bit too
It should be -remap, not +map

Re: Shrinking file size on an animated gif

Posted: 2016-02-24T17:58:35-07:00
by pricj004
Does -remap require me to have an existing colortable? I give it a filename, but it errors out because it doesn't exist?

Code: Select all

convert input.gif -coalesce -scale 700x525 -fuzz 2% +dither -layers Optimize -remap colortable.gif output.gif

Re: Shrinking file size on an animated gif

Posted: 2016-02-24T18:01:13-07:00
by fmw42
Yes it requires an existing colortable image. But I am not sure you can apply it to a multiframe image. I think you may need to apply it frame-by-frame and then recombine the frames back to an animated gif and optimize at the end. So to use it, you may need to write a loop over each coalesced frame and then recombine. Unfortunately, I have not tried -remap on an animation to be sure.

If you post your colortable.gif image, then some one could test that or write a loop to process if need be.

Have you tried increasing the fuzz value?

Re: Shrinking file size on an animated gif

Posted: 2016-02-24T18:25:09-07:00
by pricj004
Yeah I don't have a colortable image - I thought the command would autogenerate one.

And yes, I've played with the fuzz value. Increasing it lowers the file size quite aggressively, but with a noticeable loss of quality (on my image anyway).

Re: Shrinking file size on an animated gif

Posted: 2016-02-24T19:00:11-07:00
by fmw42
If your colors are pretty stable from one frame to another, you can use one frame as the colortable image. You can also reduce the number of colors with +dither -colors XX for the colortable image. That should help.

Re: Shrinking file size on an animated gif

Posted: 2016-02-24T20:38:28-07:00
by snibgo
"-remap" will remap each image in the list, resulting in a list of the same length. So it works fine with animated gifs. I would put it before "-layers optimize", not after, to give "optimize" a better chance of reducing the size.

Code: Select all

convert input.gif -coalesce -scale 700x525 -fuzz 2% +dither -remap input.gif[0] -layers Optimize output.gif

Re: Shrinking file size on an animated gif

Posted: 2016-02-24T20:49:19-07:00
by fmw42
Thanks snibgo for checking that out. It is nice to know that -remap works on all the input images.

Adding to what snibgo provided, you also could reduce the number of colors in the first frame, if desired. However, it may change your colors some. So for example:

Code: Select all

convert input.gif -coalesce -scale 700x525 -fuzz 2% +dither -remap \( input.gif[0] +dither -colors XX \) -layers Optimize output.gif
If on Windows, remove the backslashed and just use ( ... ).