Embedding chemical structure information in image files

·
Adapted (to use InChIs) from an Applescript tutorial at MacResearch. This will open a ChemDraw file, save it as PNG and SDF, generate a Standard InChI and Standard InChiKey from the SDF file, then embed that metadata into the PNG file as EXIF fields.
set cdxFilePath to "/path/to/molecule.cdx" -- edit this or use a file picker dialog
set cdxFile to POSIX file cdxFilePath as alias
set pngFile to POSIX file replaceText(".cdx", ".png", cdxFilePath)
set pngFilePath to POSIX path of pngFile
set sdfFile to POSIX file replaceText(".cdx", ".sdf", cdxFilePath)
set sdfFilePath to POSIX path of sdfFile
tell application "CS ChemDraw Ultra"
  open cdxFile
  save front document in pngFile as "image/png"
  save front document in sdfFile as "chemical/x-mdl-sdfile"
  close front document
end tell
-- could also use [Edit > Copy As...] to get InChI and InChI Key, but might as well use the standard IUPAC tool
set theInChI to (do shell script "/usr/local/bin/stdinchi-1 " & sdfFilePath & " -STDIO 2>/dev/null | grep 'InChI='")
set theInChIKey to (do shell script "/usr/local/bin/stdinchi-1 " & sdfFilePath & " -Key -STDIO 2>/dev/null | grep 'InChIKey='")
-- write EXIF fields to the PNG file
do shell script "/usr/bin/exiftool -sdf\\<='" & sdfFilePath & "' -inchi='" & theInChI & "' -inchikey='" & theInChIKey & "' '" & pngFilePath & "'"
----
on replaceText(find, replace, subject)
  set prevTIDs to text item delimiters of AppleScript
  set text item delimiters of AppleScript to find
  set subject to text items of subject
	
  set text item delimiters of AppleScript to replace
  set subject to "" & subject
  set text item delimiters of AppleScript to prevTIDs
	
  return subject
end replaceText

Exiftool needs to be installed, along with a file at ~/.Exiftool_config like this (to allow user-defined EXIF tags to be written):

%Image::ExifTool::UserDefined = (
  'Image::ExifTool::PNG::TextualData' => {
    sdf => { },
    inchi => { },
    inchikey => { },
  },
);

It also needs the IUPAC executable for generating standard InChIs to be installed.

Read the EXIF fields back out from a PNG file using exiftool -u /path/to/image.png.

See also: discussions and other tools for doing this kind of thing, from a couple of years ago.