Save plots with a background image

I need to create a histogram plot with a background image and save it in a Root file as TCanvas and possibly also in a pdf. Currently what I do is:

TCanvas *canvas = new TCanvas
TH2F *bkgHisto = new TH2F("", "", 100, -180, 180, 100, -90, 90);
bkgHisto->Draw()
TImage *bkgImg = TImage::Open("../Assets/earth.png")
bkgImg->SetConstRatio(kFALSE)
bkgImg->Draw()
canvas->Update()
canvas->Print("test.pdf")
TFile *outFile = TFile::Open("test.root", "RECREATE")
outFile->cd()
canvas->Write()
outFile->Close()

The plot is visualized correctly, but I get this error when I try to save it on a pdf file:

and the generated pdf has only a blank page. Since direct output on pdf seems to be not supported, is there a workaround to put the plot on a pdf?
Next, when I try to open the saved Root file and plot the canvas there contained I get:

and then the canvas is drawn but without the background image, it displays only the histogram. Is there a way to fix this behaviour?
I’m using Root 5.34.25.
Thanks.

It cannot be saved in a PDF you should save it in a png.

OK, thanks, I was expecting that. And what about the problem in saving it to a Root file as a TCanvas?

Same … that functionality is “image based” and works only with image formats.

I’ve figured out a way to save a TASImage in a Root file. The image must be vectorized before saving it. In this way I’m able to draw an image to a canvas, save the canvas on file and then open the file and re-draw the canvas with the image. There is only one thing that doesn’t work, and it seems related to transparent pixels. The image on the TCanvas looks like this:



Saving the canvas with the image as:

TASImage im("earth-equirect.png")
im.Vectorize()
TCanvas c1
im.Draw("X")
TFile *outFile = TFile::Open("test.root", "RECREATE")
c1.Write()
outFile->Close()

and then drawing it back as read from the root file I get:



The white area in the original image is made of transparent pixels, which are converted to black pixels when the image is vectorized, I think. It seems that the first palette entry is assigned to transparent pixels when vectorizing, and in my case this results in turning them to black. Am I doing something wrong or this is the expected behavior? I attach the original file which I used for my test, in case it might be useful to investigate the problem.
Thanks.

Yes, transparent pixels are ignored I guess in that case.
I am not sure what Vectorize does.