Depicting Molecule in JPG¶
Problem¶
You want to generate an image of your molecule in JPG format.
Ingredients¶
|
Difficulty level¶
Download¶
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 |
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | def RenderMolecule(mol, opts, ofilename):
disp = oedepict.OE2DMolDisplay(mol, opts)
ext = oechem.OEGetFileExtension(ofilename)
if oedepict.OEIsRegisteredImageFile(ext):
ofs = oechem.oeofstream()
if not ofs.open(ofilename):
oechem.OEThrow.Fatal("Cannot open output file!")
oedepict.OERenderMolecule(ofs, ext, disp)
elif ext in ['jpg', 'tiff', 'gif']:
imagestr = oedepict.OERenderMoleculeToString('png', disp)
imagefile = io.BytesIO(imagestr)
image = Image.open(imagefile)
image.save(ofilename)
|
Usage¶
Usage
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 OEDepict manual¶
Theory
- Image file formats section
API
- OE2DMolDisplayOptions class
- OE2DMolDisplay class
- OERenderMolecule function
- OEIsRegisteredImageFile function
- OEGetSupportedImageFileExtensions function