how to load mrb file and save all nodes in nii format using script

Operating system: windows 10
Slicer version:4.8.1
Expected behavior: save nodes in nii format using script
Actual behavior: only can save in nrrd format

Hi,
I want to export image and labelmap node in nii format to a directory using python script.
My script is:

mrbfile=r’D:\sample.mrb’
outdir = r’D:\output_dir’
loadScene(infilename)
saveScene(outfilename[:-4])
slicer.mrmlScene.Clear(0);

However, this could only save node in nrrd format, the 3D Slicer default format. How could I save my image node and labelmap node in nii format?

Thanks a lot.

You can iterate through all the image nodes and save them to any folder using any of the supported file extensions. Something like this:

for volumeNode in slicer.util.getNodesByClass("vtkMRMLScalarVolumeNode"):
  volumeNode.AddDefaultStorageNode()
  slicer.util.saveNode(volumeNode, "c:/tmp/" + volumeNode.GetName() + ".nii")
2 Likes

Thank you very much, Andras. It works well.

1 Like