How can I convert NIFTI files to NRRD files in command lines?

Hello,

I have 4D dynamic susceptibility contrast (DSC) dicom images (T2* with contrast agent) to analyze.
I’ve noticed that I can convert NIFTI files to NRRD files, just by click “Save” in Slicer.
However, I want to convert hundreds of NIFTI files to NRRD files, so I need to do it by command lines.
Could anyone please tell me how can I do it by command lines?
Thank you in advance!

All the best,
Kyu Sung

All operations you perform on the GUI are accessible through Python scripting, too. Save/load functions are exposed using a convenient interface in slicer.util package. You can start a script directly from the command-line as described here.

1 Like

Thank you so much.
However, I don’t understand why the code below is not working.
Could you please tell me what’s the problem? (e.g. option, syntax, etc.)

1. I just want to load 35000671_DSC.nii.gz and save as a NRRD file with the same basename (i.e. save/load problem for a single file).

MRHead=slicer.util.loadVolume(“D:\Data\SNUH_glioma\35000671_DSC.nii.gz”, returnNode=True)
head = slicer.util.getNode(‘MRHead’)
slicer.util.saveNode(head, “D:\Data\SNUH_glioma\35000671_DSC.nrrd”)

exit()

2. Moreover, I can’t find how to load files from the folder containing all the NIFTI files (hundreds of enrolled patient’s MRI) and save as NRRD files with the same basename.

Could you please tell me how to deal with this multi-files case?
capture
Thank you so much!
Best,
Kyu Sung

You can get filename from full path or get all files in a folder using standard Python commands. You can easily find answers on Google search and StackOverflow.

Thank you so much for quick response!

However, I think it’s not the problem of Python commands.
I think it’s because there’s no “slicer.py” on my Slicer folder.
I think I should “import slicer” or “import slicer.util” to use those functions such as loadVolume, getNode, and saveNode. But “import slicer” in python console makes an error saying there’s no module named ‘slicer’.

image

Moreover, I can’t find more specific usage of “slicer.util.loadVolume” from the link.
For example, does the argument “filename” mean full file path - which is I understood?

I did it on the Slicer Python interactor, then I’ve got errors like these.
image

Could you please give more specific information to solve this problem?
Thank you so much for your help!

Hi @Kyu_Sung_Choi - you need to use forward slashes for your paths in python: “d:/file” not “d:\file”

HTH,
Steve

Thank you for your quick response!

However, both ways are seemed to be not working.

image

Can anyone tell me where should I save my python file not to see the error msg saying “no modules named ‘slicer’”?
I can’t find “slicer.py” or “slicerrc.py” in Slicer folder.
Thank you in advance!

If you want to use Slicer from a Jupyter notebook, then you need to use the Slicer Jupyter kernel. See instructions here: https://github.com/Slicer/SlicerJupyter.

Within Slicer’s Python interactor, slicer.util.loadVolume works well for me. Could you try to load a different image (e.g., download a Slicer sample data set, save to disk, and try to load that). Use stable or latest nightly version of Slicer. If you have any problems then attach the full application log (menu: Help / Report a bug).

1 Like

Thank you for your quick response!
However, I’ve found that Python interactor works well on my workstation, not my desktop.

image

As you recommended, loading sample data (i.e. MRhead) and saving in NRRD file works well within Python interactor.

image

Now, let me put it in this way then,

1. Where should I save my slicer_test.py file (i.e. path)?
slicer_test.py is the simplest code below.

import slicer
slicer.util.loadVolume("/home/cndl/DSC/CBV_map/35000671_CBV.nii.gz")

I saved my slicer_test.py file in /bin/Python/slicer, where util.py in which loadVolume() function is contained.
However, running “python slicer_test.py” in command line gives me below error msg.

image

I tried to find “slicer.py” to import slicer module, which I couldn’t find at all.

2. Can I use “slicer.util.loadVolume -> slicer.util.getNode -> saveNode” functions in a row to convert NIFTI files in NRRD files?
However, above code makes an error.
I think I don’t understand what the “Node” is.
Do I have to use “loadNodeFromFile” function instead?
However, I don’t understand the argument “filetype”, which is not specific in the slicer package usage link.

image

Could you please give me more specific answer?
Thank you in advance!

Best,
Kyu Sung

It does not matter where you put your .py file. However, it is important to run it using Slicer.exe and not using Python.exe. Some features work with PythonSlicer.exe, too.

You can load volume using slicer.util.loadVolume(‘my filename.nrrd’)

1 Like

Oh, I see. What a mistake!
Refering below link, I wrote the code below and solve it by entering the below command in the command line.
https://www.slicer.org/wiki/Documentation/Nightly/ScriptRepository#Load_volume_from_file

import slicer
[success, loadedVolumeNode] = slicer.util.loadVolume("/home/cndl/DSC/CBV_map/35000671_CBV.nii.gz", returnNode=True)
slicer.util.saveNode(loadedVolumeNode, “/home/cndl/DSC/CBV_map/35000671_CBV.nrrd”)

./Slicer --no-main-window --no-splash --python-script slicer_test.py

However, I’ve noticed I have to load 4D DSC images, which cannot be done by util.loadVolume() function.
Should I use MultiVolumeImporter then?
However, I can’t find any function by that name on the link you posted.
Please let me know what function should I use to load 4D DSC images in Python script?
So sorry to bother you.
Thank you so much!

@Kyu_Sung_Choi please see this post for the answer to your last question: How can I import NIFTI files to DSCMRIAnalysis module?

Hi, Is it possible to convert 4D dicom images to a single nrrd file using the slicer/python command line? I want to loop over a collection of 4d data and convert each to nrrd or nifti for analysis.

I tried slicer.util.LoadVolume but am trying to split the data across the acquisition time. Is there an equivalent multivolumeimporter python call ?

for key,value in fileDict.items():
if ( value[‘SeriesModality’] == ‘MR’):
node=slicer.util.loadVolume(value[‘dcmfile’],returnNode=True);
outputdir = ‘tmpconvert/%04d%03d/’ % (int(value[‘PatientNumber’]) ,value[‘StudyNumber’])
print( outputdir )
os.system('mkdir -p %s ’ % outputdir )
slicer.util.saveNode(node[1], ‘%s/%s.nii.gz’ % (outputdir,value[‘seriesanonuid’] ) )
exit()

thanks!

slicer.util.LoadVolume supports very basic DICOM image import, but it is generally not recommended for loading DICOM data. It cannot load 4D data sets.

Yes, this should be no problem at all. You can load a 4D volume from DICOM and then save it as nrrd.

If you really just need to convert images from DICOM to nrrd then you can also use standalone command-line tools, such as dcm2niix.

1 Like