Optimizing an animated gif. Very bad result

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
ravel

Optimizing an animated gif. Very bad result

Post by ravel »

Hi,

i am trying to reduce the size of an animated gif with IM without great success.
This is the image: http://img3.imagebanana.com/img/vmqi06u/test.gif

I used the command:
convert source.gif -layers optimizeplus dest.gif

The size of the gif gets reduced from 247 kb to 245 kb. a bit disappointing.
what am i doing wrong?

i tested a tool called SuperGIF which reduces the size of this gif to 44 kb. unfortunately SuperGIF has no command line modus.
is IM able to archive similar results in gif compression?

thanks in advance
ravel
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: Optimizing an animated gif. Very bad result

Post by anthony »

I took at look and yes it is optimizing very slightly, but not well.

What I think is happing is that the background image is slightly different from one frame to another. It was probably a JPEG image and each frame was generated and saved separatally causing slight quantization/dither or JPEG lossy differences in pixel colors.

In fact if you animate the original you can SEE the color banding changing in the background image, even though it is still the same image. As it is VISIBLE the color changes thought the background image must be VERY SEVER!

You can tell IM to treat 'near' colors as being the same by using a -fuzz (fuzz factor) See IM Examples
http://www.imagemagick.org/Usage/color/#fuzz

Adding a -fuzz 2% produced a better optimization, but still not very good. At -fuzz 15% It isolated the differences for frame optimization to just the visible color band changes I noted before.
At 25% the differences were almost to just the text changes.

Finally at a massive 30% fuzz factor (ignore color changes below that figure, did it optimize to just the text changes.

Code: Select all

convert test.gif -fuzz 30% -layers Optimize result.gif
test.gif -> 253621 bytes
result.gif -> 60324 bytes
or 1/4 the size! very good for a 4 frame animation. More frames would have even larger savings.

I would suggest you look at how you are creating your animation. It is VERY VERY BAD. Try to remove the use of low quality intermediate image file formats (like GIF and JPEG), and the optimization (and final quality) of your animation show improve by default, enormously, without need for a large (or even ANY) fuzz factor.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
ravel

Re: Optimizing an animated gif. Very bad result

Post by ravel »

Hi anthony,

thanks for the quick answer. the result is great!

You are right when you say the gif is created in a very unoptimized way. Hence I want to create the gif with IM.

I found an example for creating an animated gif with IM. The result was about the same like my approach creating the GIF without IM.

Here is the source image: http://img3.imagebanana.com/img/u44c84vr/wow.jpg

I found the animated gif creating code here (last example on the page): http://www.ioncannon.net/linux/81/5-ima ... es-part-1/

This is my code for creating the animated gif:

Code: Select all

convert c:\wow.jpg -font Arial -fill white -pointsize 20 -annotate +50+50 "Frame 1" wow_frame1.gif 
convert c:\wow.jpg -font Arial -fill white -pointsize 20 -annotate +50+50 "Frame 2" wow_frame2.gif
convert c:\wow.jpg -font Arial -fill white -pointsize 20 -annotate +50+50 "Frame 3" wow_frame3.gif
convert c:\wow.jpg -font Arial -fill white -pointsize 20 -annotate +50+50 "Frame 4" wow_frame4.gif
convert -delay 100    -page +0+0 wow_frame1.gif    -page +0+0 wow_frame2.gif    -page +0+0 wow_frame3.gif    -page +0+0 wow_frame4.gif    -loop 0 c:/wow_animation.gif
Result: 248 kb size.

What am i doing wrong?

Thanks,
ravel
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: Optimizing an animated gif. Very bad result

Post by anthony »

ravel wrote:Hi anthony,

thanks for the quick answer. the result is great!

You are right when you say the gif is created in a very unoptimized way. Hence I want to create the gif with IM.

I found an example for creating an animated gif with IM. The result was about the same like my approach creating the GIF without IM.

Here is the source image: http://img3.imagebanana.com/img/u44c84vr/wow.jpg

I found the animated gif creating code here (last example on the page): http://www.ioncannon.net/linux/81/5-ima ... es-part-1/

This is my code for creating the animated gif:

Code: Select all

convert c:\wow.jpg -font Arial -fill white -pointsize 20 -annotate +50+50 "Frame 1" wow_frame1.gif 
convert c:\wow.jpg -font Arial -fill white -pointsize 20 -annotate +50+50 "Frame 2" wow_frame2.gif
convert c:\wow.jpg -font Arial -fill white -pointsize 20 -annotate +50+50 "Frame 3" wow_frame3.gif
convert c:\wow.jpg -font Arial -fill white -pointsize 20 -annotate +50+50 "Frame 4" wow_frame4.gif
convert -delay 100    -page +0+0 wow_frame1.gif    -page +0+0 wow_frame2.gif    -page +0+0 wow_frame3.gif    -page +0+0 wow_frame4.gif    -loop 0 c:/wow_animation.gif
Result: 248 kb size.

What am i doing wrong?

Thanks,
ravel
First you can improve the result by changing from GIF intermediate images to PNG. the image format does not matter as long as it is not lossy (GIF and JPEG are lossy formats, they loose and change the image when saving due to the format specification).

as you are not saving with any offsets, the format does not need to be special. Any non-lossy image format will do.

Code: Select all

convert c:\wow.jpg -font Arial -fill white -pointsize 20 -annotate +50+50 "Frame 1" wow_frame1.png
convert c:\wow.jpg -font Arial -fill white -pointsize 20 -annotate +50+50 "Frame 2" wow_frame2.png
convert c:\wow.jpg -font Arial -fill white -pointsize 20 -annotate +50+50 "Frame 3" wow_frame3.png
convert c:\wow.jpg -font Arial -fill white -pointsize 20 -annotate +50+50 "Frame 4" wow_frame4.png
convert -delay 100   wow_frame[1-4].png    -loop 0 c:\wow_animation.gif
that simple change => 255461 bytes
Optimize

Code: Select all

convert c:\wow_animation.gif -layers optimize c:\wow_anim_opt.gif
and you get 199037 bytes
But that still is not so good.

That is due to 'dither' failures, again because it was saved to a lossy format BEFORE the frame optimization was performed. To avoid that I replaced that final step in the first set of commands with...

Code: Select all

convert -delay 100   wow_frame[1-4].png   -layers optimize -loop 0 c:\wow_anim_opt2.gif
so that its is optiomized BEFORE being saved to GIF for the first time.

Size is now 64297 bytes, and no 'fuzz factor' was required as the color differences from one frame to the next was exact!

Even better, use the techniques simular to that shown in IM examples (which is probbaly a lot better than the docs you were using) for modifying frames,
http://www.imagemagick.org/Usage/anim_mods/#frame_mod
to create the WHOLE animation in one command!

Code: Select all

convert -delay 100 c:\wow.jpg \
   -font Arial -fill white -pointsize 20 \
    \( -clone 0 -annotate +50+50 "Frame 1" \) \
    \( -clone 0 -annotate +50+50 "Frame 2" \) \
    \( -clone 0 -annotate +50+50 "Frame 3" \) \
    \( -clone 0 -annotate +50+50 "Frame 4" \) \
   -delete 0   -layers optimize \
   -loop 0 c:\wow_anim_opt2.gif
No intermediate frames, not dither or color differences, and even a nicely color dithered constant background that does not even have the color banding that was present on the original animation example you gave. Just a clean well optimized static background with writing on each and every frame.

NOTE: my own examples will be a little larger than what you will get due to a 'annotate slew' bug that is present in my current the beta release version of ImageMagick.

ASIDE: see IM Examples, API, Window DOS scripts for some techniques on how to convert my Linux shell style to DOS
http://www.imagemagick.org/Usage/api/#windows
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
Post Reply