Generating Transparent PNG

Problem

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

Ingredients

Difficulty Level

../_images/chilly.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.

1def RenderMolecule(mol, opts, filename):
2
3    disp = oedepict.OE2DMolDisplay(mol, opts)
4    clearbackground = False
5    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.

1def RenderMoleculeToImage(mol, opts, filename):
2
3    image = oedepict.OEImage(opts.GetWidth(), opts.GetHeight(), oechem.OETransparentColor)
4
5    disp = oedepict.OE2DMolDisplay(mol, opts)
6    clearbackground = False
7    oedepict.OERenderMolecule(image, disp, clearbackground)
8
9    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