1

I'm using Magick.net to resize down the dimension of an animated GIF, but the smaller dimension-ed file ends up with larger file size.

Original file: 500 x 225, 443KB

Resized file: 400 x 180, 488KB

Here is my code

using (var imageColl = new MagickImageCollection(source))
{
  widthOrig = imageColl[0].Width;
  heightOrig = imageColl[0].Height;
  imageColl.Coalesce();
  imageColl.Optimize();
  imageColl.OptimizeTransparency();

  foreach (MagickImage image in imageColl)
  {
    var (width, height) = GetNewSize(widthOrig, heightOrig, 400);

    image.Resize(width, height);
  }

  // saving file omitted
}

Could someone point out what I'm missing to get smaller file size after resizing it? Thank you.

Ray
  • 12,101
  • 27
  • 95
  • 137
  • The reason is that GIF contains a limited number of colors (max 256). If you start with say 8 color and resize, the resize interpolates between neighboring pixels and produces new colors. Unless you limit the colors again back to 8, your file will have more colors and thus be larger in size. – fmw42 Aug 15 '18 at 18:14
  • @fmw42 What exactly is the code to make that happen? Thanks. – Ray Aug 15 '18 at 18:25
  • I don't know the Windows port, but would imagine you need to move your two `Optimize()` calls **after** `Resize()`. – Mark Setchell Aug 15 '18 at 22:12
  • I tried call the two Optimize after Resize, didn't work. – Ray Aug 17 '18 at 12:51

1 Answers1

2

The reason is that GIF contains a limited number of colors (max 256). If you start with fewer colors and resize, the resize interpolates between neighboring pixels and produces new colors. Unless you limit the colors again back to your original colors, your file will have more colors and thus be larger in size

For a single gif image, in Imagemagick, you could first make a color table image of the unique colors in your input.

convert input.gif -unique-colors  colortable.gif


(See https://www.imagemagick.org/Usage/quantize/#extract).

Then use -remap to after resizing to apply the same colors back into the resized image.

convert input.gif -resize WxH +dither -remap colortable.gif resized.gif


(See https://www.imagemagick.org/Usage/quantize/#remap)

If have an animated gif, then you need to find the number of colors of each frame and create one colorable for each. Or create one colorable that spans the colors in your animation. Then you would separate the frames of the animation and apply colorable to each frame. Then combine the frames back into an animation.

The same concept is applied to convert a movie to a gif animation at this link -- https://www.imagemagick.org/Usage/video/#gif. Note that in this link -map is an ancient terminology for -remap. So use -remap.

fmw42
  • 46,825
  • 10
  • 62
  • 80
  • 1
    Thank you for your answer. Do you know how to write it in C#? – Ray Aug 17 '18 at 12:53
  • Sorry, I only know the Imagemagick command line. But you can ask at https://www.imagemagick.org/discourse-server/. There is a Magick.NET forum or in the Users forum. – fmw42 Aug 17 '18 at 14:56