How to LZW compression for 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?".
abhishekv1

How to LZW compression for Gif

Post by abhishekv1 »

Hi guys,

Please tell me how can I enable LZW compression to
compress my gif animations. I am resizing 500x500
gif animation sizes to 176x176 but the file size is becoming
very large. On research I found that I have to enable LZW
compression to compress gifs.
Please tell me how can I do that.


Regards,
Abhishek
User avatar
magick
Site Admin
Posts: 11064
Joined: 2003-05-31T11:32:55-07:00

Post by magick »

Modern versions of ImageMagick have LZW compression enabled so you can simply upgrade to the latest release, ImageMagick 6.2.6-7.
abhishekv1

Post by abhishekv1 »

I was using ImageMagick 6.2.4. However on your instruction I upgraded to 6.2.7.

After installation I typed
convert -list format

but the output did not have the LZW option.

I even tried this:

convert -compress LZW car.gif car_1.gif

but file size does not get compressed.

Please help.

regards,
Abhishek
User avatar
magick
Site Admin
Posts: 11064
Joined: 2003-05-31T11:32:55-07:00

Post by magick »

Your image is being compressed, perhaps not at a size you want but none-the-less its compressed. Post a URL to your car.gif image and we will download and try to reproduce the problem you are having.
abhishekv1

Post by abhishekv1 »

Hi,

I do not have web space to put my image on a url.
But I think I will explain to you my main concern.
Basically I am trying to scale an animation of 500x500 to
176x176,176x178,208x208,208x144..etc. What I am doing to
achieve the same is as follows:

-spit the animation into frames using 'coalesce' and 'adjoin'.
-crop (if required) and scale each frame one by one to the
required dimension using 'scale'.
-combine the frame to form the resized animation using 'adjoin'.
-then to optimize the animation I process it with 'gifsicle -b -O2'

Though the output was very good, the size shoots up beyond limits.
I tried using 'sample' instead of 'scale', though the size went down
considerably, the quality of the output was not impressive.

Please tell me if the process I am following to resize animation
is correct. Or if you can tell me a better method which can solve
my problem, plz do.

Regards,
Abhishek
abhishekv1

Post by abhishekv1 »

hi,

Please reply to my previous query posted on
Sat May 20, 2006 12:08 pm

I need your help.

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

Post by anthony »

More than likely you are having some color issues. LWZ compression does NOT compress the color tables of an image and if the color tables of each frame are different (likely) than you will have a large color tabe attached to each and every frame of your animation.

Resizing GIF animations is difficult, but usally due to transparency, It generates lots of extra colors! Only some rough notes on this is in IM Examples..
http://www.cit.gu.edu.au/~anthony/graph ... ns/#resize

As for color table merging see the optimization notes in
http://www.cit.gu.edu.au/~anthony/graph ... ics/#color

You may like to look at the rest of the page, especially the next section.

Color optimization should be performed BEFORE the image sequence is saved to GIF (whcih has its own non-global color optimization).
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
abhishekv1

Post by abhishekv1 »

hi,

Thx a lot Anthony. I guess the best way is to do the optimization during
creation of animation. Last whole week went by researching all over the net to find how to reduce filesize of resized gifs. I downloaded all sorts of tools and functionalities available but none were impressive.

Anyways, taking your advice, I reduced the frame count to less than 20. Earlier they used to be between 54-90. I also reduced the colors used. Now after doing that, my resized gifs are having a considerable reduction in filesize.

But there are some animations still which are going up to 600-800 KB.
e.g for some animations when I resize 375x500 to 240x320 using scale, I get size upt 650KB. Is there any way I can restrict the size upto 400KB or atleast lessen the size by 100-200KB.

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

Post by anthony »

You will have to try and figure out why it is still using a lot of space.

What are the frame sizes, which is the number one cause of space in the image. Are all the frames sharing a single globle color table, or still have a seperate color table per image. How many colors in the color table?
Just how different are each frame, small or lareg scale?

Basicaly you will need to study the image, in much the same way I studied my 'problem animation' on the IM Examples page.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
abhishekv1

Post by abhishekv1 »

Hi,

I examined the animation.
I have 15 frames of size 375x500 in my animation called 'FishPot.gif'.

I do the following:

Split :
convert FishPot.gif -coalesce FP_coal.gif
convert FP_coal.gif +adjoin FP_%03d.gif

Resize:
convert -quality 100 -antialias -scale 240x320! FP_000.gif FP_000.gif
convert -quality 100 -antialias -scale 240x320! FP_001.gif FP_001.gif
convert -quality 100 -antialias -scale 240x320! FP_002.gif FP_002.gif
convert -quality 100 -antialias -scale 240x320! FP_003.gif FP_003.gif
convert -quality 100 -antialias -scale 240x320! FP_004.gif FP_004.gif
convert -quality 100 -antialias -scale 240x320! FP_005.gif FP_005.gif
convert -quality 100 -antialias -scale 240x320! FP_006.gif FP_006.gif
convert -quality 100 -antialias -scale 240x320! FP_007.gif FP_007.gif
convert -quality 100 -antialias -scale 240x320! FP_008.gif FP_008.gif
convert -quality 100 -antialias -scale 240x320! FP_009.gif FP_009.gif
convert -quality 100 -antialias -scale 240x320! FP_010.gif FP_010.gif
convert -quality 100 -antialias -scale 240x320! FP_011.gif FP_011.gif
convert -quality 100 -antialias -scale 240x320! FP_012.gif FP_012.gif
convert -quality 100 -antialias -scale 240x320! FP_013.gif FP_013.gif
convert -quality 100 -antialias -scale 240x320! FP_014.gif FP_014.gif

Join:
convert -delay 8x100 FP_000.gif -delay 8x100 FP_001.gif -delay 8x100 FP_002.gif -delay 8x100 FP_003.gif -delay 8x100 FP_004.gif -delay 8x100 FP_005.gif -delay 8x100 FP_006.gif -delay 8x100 FP_007.gif -delay 8x100 FP_008.gif -delay 8x100 FP_009.gif -delay 8x100 FP_010.gif -delay 8x100 FP_011.gif -delay 8x100 FP_012.gif -delay 8x100 FP_013.gif -delay 8x100 FP_014.gif -adjoin -loop 0 FishPot_240x320.gif

Optimize:
gifsicle -b -O2 FishPot_240x320.gif

What I saw was when use '-coalesce' on the animation, filesize of each of the frame enlarges to a large extent. But when I directly use '+adjoin' without using '-coalesce', the filesize of each frame remains normal, but the output produced then is not right. What can I use instead of '-coalesce' in order to keep the frame filesizes low?
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Post by anthony »

You have not merged the color tables of the resize images. Also DO NOT use GIF for the intermedita images as it has its own color reduction schemes that will interfer. You don't even need to use intermediate images
as scale can handle the whole sequence. Finally -quality is not used for GIF.

See
http://www.cit.gu.edu.au/~anthony/graph ... ics/#color

EG:
coalesce, scale, output a colormap, recolor, and re-combine.

Code: Select all

  convert FishPot.gif -coalesce antialias -scale 240x320! \
         \( -clone 0--1 -append -colors 250 -write colormap.png +delete \) \
         -map colormap.png -layer optimize FishPot_240x320.gif 
   gifsicle -b -O2 FishPot_240x320.gif 
That should do the job, though I am not sure about if -recolor and -map
are operators, or output settings. If that latter then you may need to break it up more.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
abhishekv1

Post by abhishekv1 »

Vow!! this single command is too good, it will reduce the amt of time taken to resize considerably.
Well, I copy pasted the command you sent, though it created the resized animation, it gave the following errors:

convert: unable to open image `antialias': No such file or directory.
[Abhishek@AbhiDev test_gif_size]$ \( -clone 0--1 -append -colors 250 -write colormap.png +delete \) \
-bash: (: command not found
[Abhishek@AbhiDev test_gif_size]$ -map colormap.png -layer optimize FishPot_240x320.gif
-bash: -map: command not found
[Abhishek@AbhiDev test_gif_size]$ gifsicle -b -O2 FishPot_240x320.gif
gifsicle: warning: so many colors that local colormaps were required
gifsicle: (You may want to try `--colors 256'.)


After doing the above I checked the size:
[Abhishek@AbhiDev test_gif_size]$ ll FishPot.gif FishPot_240x320.gif
-rw-rw-r-- 1 mauj mauj 515192 May 25 10:52 FishPot_240x320.gif
-rw-rw---- 1 mauj mauj 230999 May 25 10:30 FishPot_375x500.gif

How can I rectify the command so that errors are not there.
Maybe then the filesize will decrease further to within the limits I want.

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

Post by anthony »

Actually I didn't run it, just built it from memory, and IM examples.

Note what that their is no space after the \ at the end of the line, or just remove that \ and make it all one line. Also looks like I neglicted the + for
+antialias.

The command however is only an example. If you break it up use the multi-image MIFF format for the intermedite image files (it will be large).

Basically the -coalesce will make the animation a film like strip of full frames.
the Oppiste command is -layer optimize. Prehaps with or without some extra -fuzz factor for color comparision matching.

See the GIF optimization notes I pointed to.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
abhishekv1

Post by abhishekv1 »

Hey ! it's great !! I first broke it up to individually coalesce, create colormap and scale.
It reduced very much.
Then I took your advice and did everything in 1 line like this :

convert FishPot_375x500.gif -coalesce -antialias -scale 240x320! \( -clone 0--1 -append -colors 250 -write colormap.gif +delete \) -map colormap.gif -layers optimize FishPot_240x320.gif

And then gifsicle optimization to reduce it further:
gifsicle -b -O2 FishPot_240x320.gif

It came down from 513KB to 397KB. Fantastic. I think that will do. Thx a lot.

But there is one more thing. The reason I was splitting animation and working on each frame is that for some sizes, I do not directly scale them to resize. For some I have to chop a part of image and then
resize; -scale can handle the whole sequence but can -chop also ??
e.g to convert 500x375 animation to 101x80, to get the similar ratio before scaling, for each frame I do this:

convert -antialias -gravity east -chop 13x0 FishPot_001.gif FishPot_001.gif
convert -antialias -gravity west -chop 14x0 FishPot_001.gif FishPot_001.gif
convert -antialias -scale 101x80! FishPot_001.gif FishPot_001.gif

Like this I go on for all the 15 frames and then join.
So like scale can I use -chop on the animation directly without splitting and without quality loss?
abhishekv1

Post by abhishekv1 »

Voala !! it's working....the chopping also works on the whole sequence.... I tried this:

convert FishPot_500x375.gif -coalesce -gravity east -chop 13x0 -gravity west -chop 14x0 -antialias -scale 101x80! \( -clone 0--1 -append -colors 250 -write colormap.gif +delete \) -map colormap.gif -layers optimize FishPot_101x80.gif

and then the gifsicle optimization:
ll FishPot_500x375.gif FishPot_101x80.gif

File size reduces from 75.9KB to 58.8KB.

Unbelievable.....Amazing...
Thx a lot Anthony and team....You had been great....
Sorry for troubling you so much...Thx for being patient with me for for so
long.
Thx a lot !!

Cheers,
Abhishek
Post Reply