indigo.generateImageAsBase64() - Generate Image as Base64

Q

How to generate images of a given structure as a Base64 encoded string with the indigo.generateImageAsBase64() method?

✍: FYIcenter.com

A

If you want to generate an image of a given structure in PNG, JPEG, or SVG format and Base64 encoded, you can use the indigo.generateImageAsBase64() method on the Ketcher Indigo interface.

Here is an HTML document that shows you how to export the chemical structure from the Ketcher editor and generate an image.

<html>
<!-- indigo-generate-image-as-base64.html Copyright (c) FYIcenter.com. -->
<head>
<title>Generate Image as Base64 Encoded String</title>
</head>
<body>
<p>Create a chemical structure, then click generate button below the editor:</p>
<iframe id=frmKetcher src="/ketcher/index.html"
  style="width: 680px; height: 400px;"></iframe>

<p><button onclick="generate();">Generate Image</button></p>
<p>SMILES:</p>
<pre id=smiles style="background-color: #ddd;"></pre>
<p>Output:</p>
<img id=output />
<script>
  var ketcher = null;

function generate() {
  frm = document.getElementById("frmKetcher");
  ketcher = frm.contentWindow.ketcher;
  indigo = ketcher.indigo;

  promiseSmiles = ketcher.getSmiles();
  promiseOutput = promiseSmiles.then(consumeSmiles, consumeSmiles);
  promiseOutput.then(consumeOutput, consumeOutput);
}

function consumeSmiles(result) {
  document.getElementById("smiles").innerHTML = result;
  opts = {outputFormat: "png", backgroundColor: "255, 255, 255"};
  return indigo.generateImageAsBase64(result, opts); 
}

function consumeOutput(result) {
  src = "data:image/png;base64,"+result;
  document.getElementById("output").src = src;
}
</script>
</body>
</html>

Open the above HTML document on your local Web server. Draw a molecule in the editor.

Click "Generate Image" button, you will see that the SMILES representation of the molecule and its image are displayed:

Generate Image as Base64 String
Generate Image as Base64 String

 

indigo.convert() - Convert Structure Formats

indigo.calculate() - Calculates Chemical Properties

Ketcher Indigo Interface

⇑⇑ Ketcher - Chemical Structure Editor

2023-11-09, 733🔥, 0💬