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

**URL:** <https://discourse.slicer.org/t/how-can-i-convert-nifti-files-to-nrrd-files-in-command-lines/3631>\
**Category:** Support\
**Created:** [August 2, 2018, 2:09am UTC](https://discourse.slicer.org/t/how-can-i-convert-nifti-files-to-nrrd-files-in-command-lines/3631 "2018-08-02T02:09:01Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![Kyu\_Sung\_Choi](https://avatars.discourse-cdn.com/v4/letter/k/b77776/32.png) [@Kyu\_Sung\_Choi](https://discourse.slicer.org/u/Kyu_Sung_Choi)\
**Post date:** [August 2, 2018, 2:09am UTC](https://discourse.slicer.org/t/how-can-i-convert-nifti-files-to-nrrd-files-in-command-lines/3631/1 "2018-08-02T02:09:01Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![lassoan](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/lassoan/32/13_2.png) [@lassoan](https://discourse.slicer.org/u/lassoan)\
**Post date:** [August 2, 2018, 7:45am UTC](https://discourse.slicer.org/t/how-can-i-convert-nifti-files-to-nrrd-files-in-command-lines/3631/2 "2018-08-02T07:45:53Z")

</div>

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](https://slicer.readthedocs.io/en/latest/developer_guide/slicer.html). You can start a script directly from the command-line as described [here](https://www.slicer.org/wiki/Documentation/Nightly/Developers/Python_scripting#How_can_I_run_slicer_operations_from_a_batch_script.3F).

---

<div class="post-metadata">

**Author:** ![Kyu\_Sung\_Choi](https://avatars.discourse-cdn.com/v4/letter/k/b77776/32.png) [@Kyu\_Sung\_Choi](https://discourse.slicer.org/u/Kyu_Sung_Choi)\
**Post date:** [August 2, 2018, 8:26am UTC](https://discourse.slicer.org/t/how-can-i-convert-nifti-files-to-nrrd-files-in-command-lines/3631/3 "2018-08-02T08:26:48Z")

</div>

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](https://us1.discourse-cdn.com/flex002/uploads/slicer/original/3X/6/f/6fb709cf7b7054150a949002755e31cfb5ddb381.png)  
Thank you so much!  
Best,  
Kyu Sung

---

<div class="post-metadata">

**Author:** ![lassoan](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/lassoan/32/13_2.png) [@lassoan](https://discourse.slicer.org/u/lassoan)\
**Post date:** [August 2, 2018, 9:44am UTC](https://discourse.slicer.org/t/how-can-i-convert-nifti-files-to-nrrd-files-in-command-lines/3631/4 "2018-08-02T09:44:54Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Kyu\_Sung\_Choi](https://avatars.discourse-cdn.com/v4/letter/k/b77776/32.png) [@Kyu\_Sung\_Choi](https://discourse.slicer.org/u/Kyu_Sung_Choi)\
**Post date:** [August 2, 2018, 11:33am UTC](https://discourse.slicer.org/t/how-can-i-convert-nifti-files-to-nrrd-files-in-command-lines/3631/5 "2018-08-02T11:33:55Z")

</div>

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](https://us1.discourse-cdn.com/flex002/uploads/slicer/original/3X/7/9/792bab264fcbfe8bdc29c65194f1da0fd80b835f.png)

Moreover, I can’t find more specific usage of “slicer.util.loadVolume” from the [link](https://slicer.readthedocs.io/en/latest/developer_guide/slicer.html#module-slicer.util).  
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](https://us1.discourse-cdn.com/flex002/uploads/slicer/original/3X/a/b/ab51fde11dcfd6c8b96034d5a575278cd0f38dff.png)

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

---

<div class="post-metadata">

**Author:** ![pieper](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/pieper/32/8_2.png) [@pieper](https://discourse.slicer.org/u/pieper)\
**Post date:** [August 2, 2018, 11:39am UTC](https://discourse.slicer.org/t/how-can-i-convert-nifti-files-to-nrrd-files-in-command-lines/3631/6 "2018-08-02T11:39:33Z")

</div>

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

HTH,  
Steve

---

<div class="post-metadata">

**Author:** ![Kyu\_Sung\_Choi](https://avatars.discourse-cdn.com/v4/letter/k/b77776/32.png) [@Kyu\_Sung\_Choi](https://discourse.slicer.org/u/Kyu_Sung_Choi)\
**Post date:** [August 2, 2018, 11:42am UTC](https://discourse.slicer.org/t/how-can-i-convert-nifti-files-to-nrrd-files-in-command-lines/3631/7 "2018-08-02T11:42:58Z")

</div>

Thank you for your quick response!

However, both ways are seemed to be not working.

 ![image](https://us1.discourse-cdn.com/flex002/uploads/slicer/original/3X/1/d/1d826f301260b0cafda39b52e68b40881b89d3ad.png)

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!

---

<div class="post-metadata">

**Author:** ![lassoan](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/lassoan/32/13_2.png) [@lassoan](https://discourse.slicer.org/u/lassoan)\
**Post date:** [August 2, 2018, 12:37pm UTC](https://discourse.slicer.org/t/how-can-i-convert-nifti-files-to-nrrd-files-in-command-lines/3631/8 "2018-08-02T12:37:39Z")

</div>

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](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).

---

<div class="post-metadata">

**Author:** ![Kyu\_Sung\_Choi](https://avatars.discourse-cdn.com/v4/letter/k/b77776/32.png) [@Kyu\_Sung\_Choi](https://discourse.slicer.org/u/Kyu_Sung_Choi)\
**Post date:** [August 3, 2018, 12:20am UTC](https://discourse.slicer.org/t/how-can-i-convert-nifti-files-to-nrrd-files-in-command-lines/3631/9 "2018-08-03T00:20:56Z")

</div>

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

 ![image](https://us1.discourse-cdn.com/flex002/uploads/slicer/original/3X/c/9/c9dbf5b59539008c2c3ffc0489a4897492c9a29b.png)

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

 ![image](https://us1.discourse-cdn.com/flex002/uploads/slicer/original/3X/6/3/638e021e0a483d037d48a86de1394e5ada52b04e.png)

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](https://us1.discourse-cdn.com/flex002/uploads/slicer/original/3X/9/a/9a46c55f620018d5decc021476a927e939614353.png)

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](https://us1.discourse-cdn.com/flex002/uploads/slicer/original/3X/c/0/c014ac6ff300918e046ed33682e8559fa9d698f3.png)

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

Best,  
Kyu Sung

---

<div class="post-metadata">

**Author:** ![lassoan](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/lassoan/32/13_2.png) [@lassoan](https://discourse.slicer.org/u/lassoan)\
**Post date:** [August 3, 2018, 9:49am UTC](https://discourse.slicer.org/t/how-can-i-convert-nifti-files-to-nrrd-files-in-command-lines/3631/10 "2018-08-03T09:49:50Z")

</div>

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’)

---

<div class="post-metadata">

**Author:** ![Kyu\_Sung\_Choi](https://avatars.discourse-cdn.com/v4/letter/k/b77776/32.png) [@Kyu\_Sung\_Choi](https://discourse.slicer.org/u/Kyu_Sung_Choi)\
**Post date:** [August 3, 2018, 10:36am UTC](https://discourse.slicer.org/t/how-can-i-convert-nifti-files-to-nrrd-files-in-command-lines/3631/11 "2018-08-03T10:36:01Z")

</div>

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](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](https://slicer.readthedocs.io/en/latest/developer_guide/slicer.html) 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!

---

<div class="post-metadata">

**Author:** ![fedorov](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/fedorov/32/14_2.png) [@fedorov](https://discourse.slicer.org/u/fedorov)\
**Post date:** [August 3, 2018, 9:42pm UTC](https://discourse.slicer.org/t/how-can-i-convert-nifti-files-to-nrrd-files-in-command-lines/3631/12 "2018-08-03T21:42:11Z")

</div>

@Kyu_Sung_Choi please see this post for the answer to your last question: [How can I import NIFTI files to DSCMRIAnalysis module?](https://discourse.slicer.org/t/how-can-i-import-nifti-files-to-dscmrianalysis-module/3645/2)

---

<div class="post-metadata">

**Author:** ![fuentesdt](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/fuentesdt/32/9361_2.png) [@fuentesdt](https://discourse.slicer.org/u/fuentesdt)\
**Post date:** [October 12, 2020, 3:52pm UTC](https://discourse.slicer.org/t/how-can-i-convert-nifti-files-to-nrrd-files-in-command-lines/3631/13 "2020-10-12T15:52:08Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![lassoan](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/lassoan/32/13_2.png) [@lassoan](https://discourse.slicer.org/u/lassoan)\
**Post date:** [October 12, 2020, 10:15pm UTC](https://discourse.slicer.org/t/how-can-i-convert-nifti-files-to-nrrd-files-in-command-lines/3631/14 "2020-10-12T22:15:35Z")

</div>

> [@fuentesdt](#):
>
> I tried slicer.util.LoadVolume but am trying to split the data across the acquisition time.

`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.

> [@fuentesdt](#):
>
> Is it possible to convert 4D dicom images to a single nrrd file using the slicer/python command line?

Yes, this should be no problem at all. You can [load a 4D volume from DICOM](https://www.slicer.org/wiki/Documentation/Nightly/ScriptRepository#How_to_load_DICOM_files_into_the_scene_from_a_folder) and then [save it as nrrd](https://www.slicer.org/wiki/Documentation/Nightly/ScriptRepository#How_can_I_convert_DICOM_to_NRRD_on_the_command_line.3F).

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