gPad->SaveAs("test.gif") does not always work

Hello,
I use the following code to create GIF files from PNG files

#include "TPad.h"
#include "TImage.h"
#include "TSystem.h"
#include "TString.h"
 
void testGIF(TString filename = "colour") {
 
  //Replace "colour" by "blackWhite" to see that it ROOT manage to create the gif file
   
 gSystem->Unlink(Form("%s.gif",filename.Data()));
   
 TImage *img = TImage::Open(Form("%s.png",filename.Data()));
 img->Draw();
 	
 gPad->SaveAs(Form("%s.gif",filename.Data()));
}

Here is what I get in the terminal

$ root
    ------------------------------------------------------------
   | Welcome to ROOT 6.14/04                http://root.cern.ch |
   |                               (c) 1995-2018, The ROOT Team |
   | Built for linuxx8664gcc                                    |
   | From tags/v6-14-04@v6-14-04, Aug 23 2018, 17:00:44         |
   | Try '.help', '.demo', '.license', '.credits', '.quit'/'.q' |
    ------------------------------------------------------------
 
 root [0] .x testGIF.C++("colour")
 Info in <TUnixSystem::ACLiC>: creating shared library /home/pamputt/test/./testGIF_C.so
 root [1] .x testGIF.C++("blackWhite")
 Info in <ACLiC>: unmodified script has already been compiled and loaded
 Info in <ACLiC>: it will be regenerated and reloaded!
 Info in <TUnixSystem::ACLiC>: creating shared library /home/pamputt/test/./testGIF_C.so 
 Info in <TCanvas::Print>: file blackWhite.gif has been created

My guess is ROOT is not able to convert GIF to PNG if there is too many colours in the PNG file. However using the “convert” command from ImageMagick, the GIF file is created.

The Info messages you get are not related to the gif creation. They just warn that you are compiling the same macro twice and the 2nd time it will be recompiled and reloaded. But the gif file should be created.

I agree, the Info messages only says that blackWhite.gif has been created (and it is). Yet, no message is displayed for “colour.gif” (and indeed it is not created). I wonder why ROOT does not manage to create colour.png?

I just tried. I did a siren capture of may Mac backcourt which is quite colourful. I named it “colour.png” . And I executed your macro:

$ root
   ------------------------------------------------------------
  | Welcome to ROOT 6.15/01                  https://root.cern |
  |                               (c) 1995-2018, The ROOT Team |
  | Built for macosx64 on Oct 23 2018, 14:04:30                |
  | From heads/master@v6-13-04-1856-gc5860b2c5f                |
  | Try '.help', '.demo', '.license', '.credits', '.quit'/'.q' |
   ------------------------------------------------------------

root [0] .x testGIF.C("colour")
Info in <TCanvas::Print>: file colour.gif has been created
root [1] .q

I get a gif file similar to the png.

Hmmm, weird. here it does not work. Maybe it is because this is jpg file in the message above (I uploaded png files but they have been automatically converted to jpg files). Could you try with the png files available here: ftp://ftp.cea.fr/incoming/y2k01/ROOT ?

does not work for me…

You mean you cannot connect to the link or you manage to download the PNG files and it does not work.

Sorry … the link does not work for me . I cannot access your picture.

And with these links

Try to copy/paste the links.

yes it works with copy/paste (note I also copy/pasted before). Trying you macro on colour.png…

all is fine (gif file shown with preview on the right):

Hmmm, weird. I am trying to understand why it does not work here (linux). Is the conversion from png to gif done the same way on mac and linux?

Yes. Can you try in batch mode ?

$ root -b -l
root [0] .x testGIF.C("colour")
Info in <TCanvas::Print>: gif file colour.gif has been created

Which ROOT version are you using ?

I use ROOT 6.14/04. I tried in batch mode and indeed it works. Then, I tried again in graphics mode and no GIF file is created.

Ah ok … gif files production takes a different path depending if ROOT runs in batch mode or not. In batch mode it uses LibAfterImage and in interactive mode it is a screen copy of what is on screen. I would recommend you to run in batch. May be you can turn batch mode in the macro before executing your conversion procedure and turn it back to interactive when finished. In any case it seems good to run such conversation tool in batch.

Converting the png file in batch mode is a solution. However, would it be possible to know why it does not work in graphic mode? The best would be to fix the issue or at least to add an Info message that says why the conversion is not done. I ask that because I was looking for the origin of this issue and a workaround without any success and I guess it may happen to other users.

I do not see a such problem on Mac. I will se if I can reproduce it on some linux machine.

By the way if you simply try to save interactively a Canvas as a gif … does it work ?

No. Using the “SaveAs” menu, I can save the canvas as colour.jpg picture but nothing happens if I ask to save it as colour.gif.

EDIT : BTW, I can use SaveAs to save “blackWhite.gif” (“blackWhite.jpg”, or whatever) without any issue.

Ah ! ok … so that’s not related to your example. This a more general issue. Is ROOT running locally or on a remote machine ?