Depicting Molecule in JPG

Problem

You want to generate an image of your molecule in JPG format.

Ingredients

Difficulty level

../_images/chilly.png

Download

Download code

mol2img.py

See also the Usage subsection.

Solution

The following table lists the image file formats natively supported in OEDepict TK and their associated file extensions.

Graphics File Format

Format Type

File Extension

PNG (Portable Network Graphics)

raster image

.png

SVG (Scalable Vector Graphics)

vector image

.svg

bare SVG (with no header)

vector image

.bsvg

Postscript

vector image

.ps

Encapsulated PostScript

vector image

.eps

PDF (Portable Document Format)

vector image

.pdf

Unfortunately, the jpg image format is not supported by OEDepict TK. However you can render your molecule into a png image file and then convert it to jpg, gif or tiff image types using the Pillow image library.

 1def RenderMolecule(mol, opts, ofilename):
 2
 3    disp = oedepict.OE2DMolDisplay(mol, opts)
 4
 5    ext = oechem.OEGetFileExtension(ofilename)
 6    if oedepict.OEIsRegisteredImageFile(ext):
 7
 8        ofs = oechem.oeofstream()
 9        if not ofs.open(ofilename):
10            oechem.OEThrow.Fatal("Cannot open output file!")
11        oedepict.OERenderMolecule(ofs, ext, disp)
12
13    elif ext in ['jpg', 'tiff', 'gif']:
14
15        imagestr = oedepict.OERenderMoleculeToString('png', disp)
16        imagefile = io.BytesIO(imagestr)
17        image = Image.open(imagefile)
18        image.save(ofilename)

Usage

Usage

mol2img.py

prompt > python3 mol2img.py molecule.ism molecule.jpg
prompt > python3 mol2img.py molecule.sdf molecule.png

To install Pillow in a Python 3 conda environment:

prompt > conda create -n oecookbook python=3
prompt > source activate oecookbook
(oecookbook) prompt > pip install Pillow

See also in OEChem manual

API

See also in OEDepict manual

Theory

API