Reputation: 12441
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.
Upvotes: 1
Views: 1072
Reputation: 53089
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.
Upvotes: 2