I really like the idea of having an image of a molecule that I can re-construct the molecule from. It's been possible to do this with SVGs from the RDKit for a while by adding chemical metadata. There's a desciption of that here that shows both adding the metadata and constructing the molecule from it.
An aside: Since you can now add SVGs to powerpoint documents, I was super excited about the idea of being able to have molecule images in my Powerpoint decks together with the metadata to reconstruct those molecules. Unforunately, as of this writing at least, Powerpoint strips out all the metadata that it doesn't recognize, so the chemistry information all ends up being removed. Sad...
Anyway, back to the point of this blog post: I wanted to do the same thing with the PNGs that the RDKit can generate, but kept getting stuck on how to do this from the C++ side. Today I realized that it's quite easy to do it in Python and that this would possibly be useful to most RDKit users. Thus this post.
If anyone knows of a decent C++ snippet or library with an BSD-compatible license (this rules out exiv2, which otherwise looks like the perfect thing) that shows how to add metadata to a PNG, please do let me know.
from rdkit import Chem
from rdkit.Chem import Draw
from rdkit.Chem.Draw import IPythonConsole
IPythonConsole.molSize = 350,300
from PIL import Image
from io import BytesIO
import rdkit
print(rdkit.__version__)
colchicine = Chem.MolFromSmiles('COc1cc2c(c(OC)c1OC)-c1ccc(OC)c(=O)cc1[C@@H](NC(C)=O)CC2')
colchicine
A quick reminder on what the metadata in the SVG looks like:
dm = Draw.PrepareMolForDrawing(colchicine)
d2d = Draw.MolDraw2DSVG(450,400)
d2d.DrawMolecule(dm)
d2d.AddMoleculeMetadata(dm)
d2d.FinishDrawing()
svg = d2d.GetDrawingText()
with open('/tmp/blah.svg','w+') as outf:
outf.write(svg)
!grep rdkit /tmp/blah.svg
Ok, let's move onto PNGs. Start by getting a PIL Image object with the molecule drawing. There's a convenience function for this:
img = Draw.MolToImage(dm,size=(450,400))
img
Now use the PngImagePlugin which is part of pillow to add CXSMILES as the metadata and write the PNG out to a file:
from PIL.PngImagePlugin import PngInfo
metadata = PngInfo()
metadata.add_text("RDKit_SMILES",Chem.MolToCXSmiles(dm))
img.save("/tmp/blah.png",format="PNG",pnginfo=metadata)
Confirm that we can access the metadata when we read the file back in:
nimg = Image.open('/tmp/blah.png')
nimg.text
tmol = Chem.MolFromSmiles(nimg.text['RDKit_SMILES'])
tmol
And demonstrate that this isn't just something the PIL tools can read... show the metadata using imagemagick:
!identify -verbose /tmp/blah.png | grep RDK
Since people inevitably want to put structures into Powerpoint, make sure that we can safely add these and not lose the metadata. I will use the python-pptx library to demo this.
from pptx import Presentation
from pptx.util import Mm
bio = BytesIO()
img.save(bio,format="PNG",pnginfo=metadata)
prs = Presentation()
blank_slide_layout = prs.slide_layouts[6]
slide = prs.slides.add_slide(blank_slide_layout)
left = top = Mm(50)
pic = slide.shapes.add_picture(bio, left, top, height=Mm(60))
prs.save('/tmp/test.pptx')
Make sure we don't lose the metadata:
nprs = Presentation('/tmp/test.pptx')
slide = nprs.slides[0]
pic = slide.shapes[0]
bio = BytesIO(pic.image.blob)
nimg = Image.open(bio)
nimg.text
And to be super sure, I opened the file in PPT in Office365, added some text, and then re-downloaded it
nprs = Presentation('/home/glandrum/Downloads/test.pptx')
slide = nprs.slides[0]
pic = slide.shapes[0]
bio = BytesIO(pic.image.blob)
nimg = Image.open(bio)
nimg.text
Looks good!