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 |
|
SVG (Scalable Vector Graphics) |
vector image |
|
bare SVG (with no header) |
vector image |
|
Postscript |
vector image |
|
Encapsulated PostScript |
vector image |
|
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.
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
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
OEGetFileExtension function
oeofstream class
oeosstream class
See also in OEDepict manual
Theory
Image file formats section
API
OE2DMolDisplayOptions class
OE2DMolDisplay class
OERenderMolecule function
OEIsRegisteredImageFile function
OEGetSupportedImageFileExtensions function