Describe the bug
MolToImage working just from jupyter notebook:
I am trying to include RdKit MolToImage function in a script in order to visualize the atom indeces and to decide where to attach fragments of fragment library.
As part of the script include vmd - python I am unable to update RdKit and Python 3 to the latest version due to conflicts between vmd and RdKit dependencies(mainly involving libnetcdf package), so I have to stick with python 3.6.13 and rdkit 2019.09.3.
The following code works in Jupyter notebook, displaying the molecules passed as smiles string to the function, but when I try to include the code into something that can be run directly from command line it raises the error showed below. I am pretty confident it does not regard argument parsing as it does not raise any Missing/Invalid Argument error and the help of the command works properly.
I also tried to include the image.save("mymolecule.png") to save the result as png file, but it raises the same error.
Finally, I tried to directly calling the python interpreter from terminal reproducing the example provided in the "Reading, Drawing, and Writing Molecules" in your Getting started with RdKit tutorial and the same error arises.
Do someone knows the reason behind this difference? Are there any workaround (apart from updating Python and/or Rdkit)?
Code
import json
import re
import pandas as pd
from rdkit import Chem
from rdkit.Chem import AllChem
from rdkit.Chem import Draw
from rdkit.Chem import Descriptors
def mol_with_atom_index(reference_smiles: str, reference_name: str):
mol = Chem.MolFromSmiles(reference_smiles)
for atom in mol.GetAtoms():
atom.SetAtomMapNum(atom.GetIdx())
atom_indexes = Draw.MolToImage(mol)
mol.save(f'{reference_name}.png')
return atom_indexes
Error
Traceback (most recent call last):
File "/common/users/fricci/anaconda3/envs/VMD_updateRdKit/lib/python3.6/runpy.py", line 193, in _run_module_as_main
"__main__", mod_spec)
File "/common/users/fricci/anaconda3/envs/VMD_updateRdKit/lib/python3.6/runpy.py", line 85, in _run_code
exec(code, run_globals)
File "/LOCAL_DATA/fricci/Klig/klig/klig/__main__.py", line 95, in <module>
main()
File "/LOCAL_DATA/fricci/Klig/klig/klig/__main__.py", line 45, in main
mol_with_atom_index(args.reference_smiles)
File "/LOCAL_DATA/fricci/Klig/klig/klig/r_group_enumerator.py", line 15, in mol_with_atom_index
atom_indexes = Draw.MolToImage(mol)
File "/common/users/fricci/anaconda3/envs/VMD_updateRdKit/lib/python3.6/site-packages/rdkit/Chem/Draw/__init__.py", line 101, in MolToImage
img, canvas = _createCanvas(size)
File "/common/users/fricci/anaconda3/envs/VMD_updateRdKit/lib/python3.6/site-packages/rdkit/Chem/Draw/__init__.py", line 56, in _createCanvas
canvas = Canvas(img)
File "/common/users/fricci/anaconda3/envs/VMD_updateRdKit/lib/python3.6/site-packages/rdkit/Chem/Draw/cairoCanvas.py", line 147, in __init__
imgd = getattr(image, 'tobytes', image.tostring)("raw", "BGRA")
File "/common/users/fricci/anaconda3/envs/VMD_updateRdKit/lib/python3.6/site-packages/PIL/Image.py", line 546, in __getattr__
raise AttributeError(name)
AttributeError: tostring
Describe the bug MolToImage working just from jupyter notebook:
I am trying to include RdKit MolToImage function in a script in order to visualize the atom indeces and to decide where to attach fragments of fragment library. As part of the script include vmd - python I am unable to update RdKit and Python 3 to the latest version due to conflicts between vmd and RdKit dependencies(mainly involving libnetcdf package), so I have to stick with python 3.6.13 and rdkit 2019.09.3. The following code works in Jupyter notebook, displaying the molecules passed as smiles string to the function, but when I try to include the code into something that can be run directly from command line it raises the error showed below. I am pretty confident it does not regard argument parsing as it does not raise any Missing/Invalid Argument error and the help of the command works properly. I also tried to include the image.save("mymolecule.png") to save the result as png file, but it raises the same error. Finally, I tried to directly calling the python interpreter from terminal reproducing the example provided in the "Reading, Drawing, and Writing Molecules" in your Getting started with RdKit tutorial and the same error arises. Do someone knows the reason behind this difference? Are there any workaround (apart from updating Python and/or Rdkit)?
Code
Error