Saving a .png with a transparent background - DrawFrame

Despite the many past posts on how to achieve a transparent background in ROOT, I couldn’t find anything that works on my Fedora 25 / ROOT 6.10.02 installation.

Basically, I want to draw a graph, within a TFrame I specify (currently using canvas.DrawFrame(…)) and a TGaxis on a transparent background. I’m running into two difficulties:

  • making a pad transparent works as in the old instructions (FillStyle 4000). Not sure how to do it with a DrawFrame.

  • none of the examples of how to make the canvas transparent work for me. Neither on screen, nor in a png (I haven’t tested pdf thoroughly, but nothing worked there so far). For example:

$ root -bn
   ------------------------------------------------------------
  | Welcome to ROOT 6.10/02                http://root.cern.ch |
  |                               (c) 1995-2017, The ROOT Team |
  | Built for linuxx8664gcc                                    |
  | From tag v6-10-02, 6 July 2017                             |
  | Try '.help', '.demo', '.license', '.credits', '.quit'/'.q' |
   ------------------------------------------------------------

root [0] TCanvas c1
(TCanvas &) Name: c1 Title: c1
root [1] c1.SetFillColorAlpha(kRed,0.2)
root [2] c1.SaveAs("test.png")
Info in <TCanvas::Print>: file test.png has been created

Creates a white png, rather than a semi-transparent reddish png

I have all sort of partial workarounds with imagemagick scripts. But a transparent background would make things so much easier…

Hi @harelamnon. You can use an SVG format instead. I got the reddish transparency that you ask for. I’m using 6.13/01 though. EDIT: This format works great if you are working with PDF output (Like from LaTeX), as you will have vectorial images instead of raster images.

Iif I do this:

{
   TCanvas *c1 = new TCanvas("c1", "c1",10,45,700,502);
   c1->Range(-5,-106.4438,5,957.9938);
   c1->SetFillColorAlpha(kRed,0.2);
   c1->SaveAs("test.png")
}

I get this image:

Which is a transparent red.

Thanks for your answer. Manually examining the SVG file, I see it has the correct “fill-opacity”!

Unfortunately, that doesn’t help me as I need to use these images in tools that fail to handle this semi-transparent SVG correctly. Namely PowerPoint, which can’t even read SVG files, and ImageMagick, which apparently ignores the “fill-opacity” field.

When I analyze the image you uploaded with ImageMagick’s display, it can’t even find an alpha channel. When I use it in powerpoint, it appears opaque. Running your code myself, I also get opaque images.

Maybe the image on your screen is semi-transparent, but the fact it isn’t on my screen is off-topic. I’m trying to figure out how to use ROOT to save an image file, such as a .png, with a semi-transparent image.

If I run ROOT in batch mode (no screen output) and execute the lines I sent you before I also get a transparent red in the png file generated.

What do you get on your computer when you do something like:

$ root -b -n

I get the same picture i already posted

If you need a quick solution you can try to export SVG and then use something like: http://svgtopng.com/ All of us have different outputs because we are using different root versions.

test

Thanks, that’s a viable work-around!
I’m not used to other tools out-performing ImageMagick :slight_smile:
(BTW: oddly, the alpha isn’t quite constant in this .png, but it’s
acceptable).

Meanwhile I used a workaround of plotting with matplotlib.
Their axes aren’t quite as nice as ROOT axes, but they’re acceptable.

Just in case someone googles this thread, the key lines I used for plotting
filled TGraphs in matplotlib are are something like:

def color_to_hex( root_color ):

return '{:02x}{:02x}{:02x}'.format( int( 255.999 * root_color.GetRed()

),
int( 255.999 *
root_color.GetGreen() ),
int( 255.999 *
root_color.GetBlue() ) )

I do not understand this …

We’re getting less and less ROOT-centric here, as this is a minor comment
on the output of http://svgtopng.com/

In the last image that Andres sent, the alpha values range from 32 to 51 -
they are not constant.
I see this by opening it with ImageMagick’s “display”; right clicking the
image, choosing “Image Info”,
and scrolling down to the “Alpha:” channel info.
This is merely an oddity - the mean is 50.9, so almost all the alpha values
are 51.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.