Shrinking file size on an animated gif

Questions and postings pertaining to the usage of ImageMagick regardless of the interface. This includes the command-line utilities, as well as the C and C++ APIs. Usage questions are like "How do I use ImageMagick to create drop shadows?".
Post Reply
pricj004
Posts: 5
Joined: 2016-02-24T15:22:30-07:00
Authentication code: 1151

Shrinking file size on an animated gif

Post 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.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Shrinking file size on an animated gif

Post 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
pricj004
Posts: 5
Joined: 2016-02-24T15:22:30-07:00
Authentication code: 1151

Re: Shrinking file size on an animated gif

Post 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.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Shrinking file size on an animated gif

Post by snibgo »

"-scale" instead of "-resize" will reduce the size.
snibgo's IM pages: im.snibgo.com
pricj004
Posts: 5
Joined: 2016-02-24T15:22:30-07:00
Authentication code: 1151

Re: Shrinking file size on an animated gif

Post by pricj004 »

Huh. So it does. Down to 3.8MB now. Thanks.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Shrinking file size on an animated gif

Post by fmw42 »

Code: Select all

Reducing to a single colour map using +map helped a bit too
It should be -remap, not +map
pricj004
Posts: 5
Joined: 2016-02-24T15:22:30-07:00
Authentication code: 1151

Re: Shrinking file size on an animated gif

Post 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
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Shrinking file size on an animated gif

Post 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?
pricj004
Posts: 5
Joined: 2016-02-24T15:22:30-07:00
Authentication code: 1151

Re: Shrinking file size on an animated gif

Post 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).
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Shrinking file size on an animated gif

Post 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.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Shrinking file size on an animated gif

Post 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
snibgo's IM pages: im.snibgo.com
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Shrinking file size on an animated gif

Post 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 ( ... ).
Post Reply