Generating Transparent PNG

Problem

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

Ingredients

Note

Requires OpenEye toolkits version 2013.Feb or later.

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 = OE2DMolDisplay(mol, opts)
    clearbackground = False
    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 = OEImage(opts.GetWidth(), opts.GetHeight(), OETransparentColor)

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

    OEWriteImage(filename, image)

Note

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

See Also in OEDepict Manual

Theory

API