# $Id$
#
# Copyright (C) 2006-2008 Greg Landrum
#  All Rights Reserved
#


def MolToImageFile(mol,filename,size=(300,300),kekulize=True, wedgeBonds=True):
  import MolDrawing
  try:
    from aggdraw import Draw
    from PIL import Image
    MolDrawing.registerCanvas('agg')
    useAGG=True
  except:
    import traceback
    traceback.print_exc()
    useAGG=False
    from sping.PIL.pidPIL import PILCanvas as Canvas
    canvas = Canvas(size=(300,300),name=filename)
    MolDrawing.registerCanvas('sping')
    drawer = MolDrawing.MolDrawing(canvas)
  if useAGG:
    img = Image.new("RGBA",size,"white")
    canvas = Draw(img)
    canvas.setantialias(True)
    drawer = MolDrawing.MolDrawing(canvas)

  if kekulize:
    import Chem
    mol = Chem.Mol(mol.ToBinary())
    Chem.Kekulize(mol)
    
  if not mol.GetNumConformers():
    from Chem import AllChem
    AllChem.Compute2DCoords(mol)
  
  drawer.wedgeDashedBonds=wedgeBonds
  drawer.AddMol(mol)

  if useAGG:
    canvas.flush()
    img.save(filename)
  else:
    canvas.save()
    

@@ -86,25 +86,12 @@
 mage
-File(mol,filename
+(mol
 ,siz
@@ -426,32 +426,59 @@
 ize=
-(300,300),name=filename)
+size,name='MolToImageFile')%0A    img = canvas._image
 %0A   
@@ -957,32 +957,16 @@
 ol(mol)%0A
-%0A  if useAGG:%0A  
   canvas
@@ -978,55 +978,196 @@
 h()%0A
+%0A
   
-  img.save(filename)%0A  else:%0A    canvas.save(
+return img%0A%0Adef MolToImageFile(mol,filename,size=(300,300),kekulize=True, wedgeBonds=True):%0A  img = MolToImage(mol,size=size,kekulize=kekulize,wedgeBonds=wedgeBonds)%0A  img.save(filename
 )%0A  

add MolToImage function