Advertisement
Pandaaaa906

Untitled

Aug 20th, 2021
1,118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.32 KB | None | 0 0
  1. from indigo import Indigo
  2. from indigo.renderer import IndigoRenderer
  3.  
  4. _indigo = Indigo()
  5. renderer = IndigoRenderer(_indigo)
  6. _indigo.setOption('render-output-format', 'png')
  7. _indigo.setOption('render-margins', '5, 5')
  8. _indigo.setOption('render-stereo-style', 'none')
  9. _indigo.setOption('render-superatom-mode', 'collapse')
  10. _indigo.setOption('ignore-stereochemistry-errors', True)
  11. _indigo.setOption('render-label-mode', 'terminal-hetero')
  12. _indigo.setOption('render-image-width', 1400)
  13. _indigo.setOption('render-image-height', 900)
  14.  
  15. def trim(im, margin=5):
  16.     bg = Image.new(im.mode, im.size, im.getpixel((0, 0)))
  17.     diff = ImageChops.difference(im, bg)
  18.     diff = ImageChops.add(diff, diff, 2.0, -100)
  19.     bbox = diff.getbbox()
  20.     if not bbox:
  21.         return
  22.     im = im.crop(bbox)
  23.     w, h = im.size
  24.     ret = Image.new(im.mode, (w + 2 * margin, h + 2 * margin), bg.getpixel((0, 0)))
  25.     ret.paste(im, (margin, margin))
  26.     return ret
  27.  
  28. def generate_image(renderer: IndigoRenderer, m: IndigoObject):
  29.     buffer = renderer.renderToBuffer(m)
  30.     b = BytesIO()
  31.     b.write(buffer)
  32.     im = Image.open(b)
  33.     im = trim(im)
  34.     ret = BytesIO()
  35.     if not im:
  36.         return None
  37.     im.save(ret, 'png')
  38.     return ret.getvalue()
  39.  
  40.  
  41. m = _indigo.loadMolecule('CCC')  # both smiles or moltext is ok
  42. b = generate_image(m)
  43.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement