Chem.Draw

Package Chem :: Package Draw |
[hide private]
[frames] | no frames]

# Source Code for Package Chem.Draw

     1  # $Id: __init__.py 691 2008-05-22 18:47:26Z glandrum $
     2  #
     3  # Copyright (C) 2006-2008 Greg Landrum
     4  #  All Rights Reserved
     5  #
     6
     7
     8 -def MolToImageFile(mol,filename,size=(300,300),kekulize=True, wedgeBonds=True):
     9    import MolDrawing
    10    try:
    11      from aggdraw import Draw
    12      from PIL import Image
    13      MolDrawing.registerCanvas('agg')
    14      useAGG=True
    15    except:
    16      import traceback
    17      traceback.print_exc()
    18      useAGG=False
    19      from sping.PIL.pidPIL import PILCanvas as Canvas
    20      canvas = Canvas(size=(300,300),name=filename)
    21      MolDrawing.registerCanvas('sping')
    22      drawer = MolDrawing.MolDrawing(canvas)
    23    if useAGG:
    24      img = Image.new("RGBA",size,"white")
    25      canvas = Draw(img)
    26      canvas.setantialias(True)
    27      drawer = MolDrawing.MolDrawing(canvas)
    28
    29    if kekulize:
    30      import Chem
    31      mol = Chem.Mol(mol.ToBinary())
    32      Chem.Kekulize(mol)
    33
    34    if not mol.GetNumConformers():
    35      from Chem import AllChem
    36      AllChem.Compute2DCoords(mol)
    37
    38    drawer.wedgeDashedBonds=wedgeBonds
    39    drawer.AddMol(mol)
    40
    41    if useAGG:
    42      canvas.flush()
    43      img.save(filename)
    44    else:
    45      canvas.save()
    46