Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from indigo import Indigo
- from indigo.renderer import IndigoRenderer
- _indigo = Indigo()
- renderer = IndigoRenderer(_indigo)
- _indigo.setOption('render-output-format', 'png')
- _indigo.setOption('render-margins', '5, 5')
- _indigo.setOption('render-stereo-style', 'none')
- _indigo.setOption('render-superatom-mode', 'collapse')
- _indigo.setOption('ignore-stereochemistry-errors', True)
- _indigo.setOption('render-label-mode', 'terminal-hetero')
- _indigo.setOption('render-image-width', 1400)
- _indigo.setOption('render-image-height', 900)
- def trim(im, margin=5):
- bg = Image.new(im.mode, im.size, im.getpixel((0, 0)))
- diff = ImageChops.difference(im, bg)
- diff = ImageChops.add(diff, diff, 2.0, -100)
- bbox = diff.getbbox()
- if not bbox:
- return
- im = im.crop(bbox)
- w, h = im.size
- ret = Image.new(im.mode, (w + 2 * margin, h + 2 * margin), bg.getpixel((0, 0)))
- ret.paste(im, (margin, margin))
- return ret
- def generate_image(renderer: IndigoRenderer, m: IndigoObject):
- buffer = renderer.renderToBuffer(m)
- b = BytesIO()
- b.write(buffer)
- im = Image.open(b)
- im = trim(im)
- ret = BytesIO()
- if not im:
- return None
- im.save(ret, 'png')
- return ret.getvalue()
- m = _indigo.loadMolecule('CCC') # both smiles or moltext is ok
- b = generate_image(m)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement