Path of image node

I needed to pass a path to DICOM file to a certain CLI module because that module needed metadata from the header. But to get the path depends on how the node was loaded, via drag-drop or via DICOM browser. Here is a function which works for both cases:

def PathFromNode(self, node):
  storageNode=node.GetStorageNode()
  if storageNode is not None:
      filepath=storageNode.GetFullNameFromFileName()
  else:
      instanceUIDs=node.GetAttribute('DICOM.instanceUIDs').split()
      filepath=slicer.dicomDatabase.fileForInstance(instUids[0])
  return filepath

# example invocation
node=slicer.util.getFirstNodeByName('volume1')
path=self.PathFromNode(node)
print "DICOM path="+path
1 Like

Thank you, it’s useful example. Could you please add it to the script repository?

1 Like

Added:
https://www.slicer.org/wiki/Documentation/Nightly/ScriptRepository#How_to_get_path_and_filename_of_a_loaded_DICOM_volume.3F

2 Likes