Generating Transparent PNG

Problem

You want to depict your molecule in a png image with transparent background for your presentation.

Ingredients

Difficulty Level

../_images/chilly1.png

Solution

If you are rendering a molecule directly into an image file, all you have to do is to call the OERenderMolecule function with the clearbackground = False parameter. If this parameter is True (by default) then the OEWhite color is used to clear the image before rendering the molecule.

1
2
3
4
5
def RenderMolecule(mol, opts, filename):

    disp = oedepict.OE2DMolDisplay(mol, opts)
    clearbackground = False
    oedepict.OERenderMolecule(filename, disp, clearbackground)

Discussion

In the case where you are rendering a molecule into an OEImage object, this object has to be constructed with the OETransparentColor color.

1
2
3
4
5
6
7
8
9
def RenderMoleculeToImage(mol, opts, filename):

    image = oedepict.OEImage(opts.GetWidth(), opts.GetHeight(), oechem.OETransparentColor)

    disp = oedepict.OE2DMolDisplay(mol, opts)
    clearbackground = False
    oedepict.OERenderMolecule(image, disp, clearbackground)

    oedepict.OEWriteImage(filename, image)

Note

The OEDepict TK supports transparent background for png, svg and pdf image files.

See also in OEDepict TK manual

Theory

API