Convert single files into one .nii


I have multiple single .ima files. These contain single slices info.
What I want to achieve is to merge all these into one .nii file.

What is the best way to do that?

Dear Helen,
You could use dcm2nii.

Thanks Frederic,
I want to use python for this task, can I achieve it using dcm2nii?

Not directly, but yest it’s possible, for example by a thing like that:
After that you load MRIcroGL (dcm2niix)

import os
command1 = “dcm2nix /YourDirectory/data.IMA”

The Alternatives section of the dcm2niix page lists several tools for converting DICOM images to NIfTI. The Links section lists several tools that exploit dcm2niix, many of these are Python-based scripts. I think dicom2nifti would be my recommendation for Python-based converter. In my experience, most of the tools listed in the alternatives section do pretty well for most modalities, but you want to carefully validate the converter you have special modalities. In particular, CT scans can have unequal slice spacing and gantry tilt while DWI scans have gradient information. If you are planning to use Slicer in particular, you may want to consider scripting DWIconvert, as is used my most Slicer users and therefore heavily tested. The nice nrrdify Python script shows how to use DWIconvert to create NRRD images and I think it could be easily adapted if you want NIfTI images.

1 Like

It is used for DWI import in Slicer, but various DICOM plugins are used for other information objects.

How dcm2niix can handle 4D CTs with unequal spacing, missing and unaligned slices, and gantry tilt? Does dcm2niix support CT reconstruction based on different DICOM tags (frame time, heart cycle phase, various vendor-specific tags)? Can dcm2niix can load multiple series as a 4D volume? If there are many options for interpreting a 4D data set, how dcm2niix tells the user the available options (and probability of which ones are thr most likely correct interpretations) and how the user can choose between those? Slicer has a sophisticated plugin system that can deal with all these, but it would be nice if some of this complex logic could be offloaded to external libraries.

You can run dcm2niix via heudiconv or nipype.

@fedorov and a group of us researched the various options for dicom conversion a while back and collected our notes on this project page . There’s also a docker based test environment that runs the various tools (slicer, freesurfer, dcm2niix, etc) and compares the results.

As a general rule, it’s best if you don’t need to perform lossy conversions, so keeping the original dicom data is desirable. Also storing derived data in dicom is the method many of us prefer (or are at least working towards).

1 Like

@lassoan I recognize Slicer has several strategies for handling DICOMs. My response was specific to the original post regarding converting files to a single .nii file.

With regards to special situations encountered with DICOMs, you may want to look at the samples that can be downloaded from the dcm2niix web page. This provides exemplars of archival images, CT scans, DWI scans, and special situations.

The web page also describes how dcm2niix handles various situations, including those described below.

How dcm2niix can handle 4D CTs with unequal spacing, missing and unaligned slices, and gantry tilt?

The NIfTI format requires equal slice spacing, while CT scans are sometimes acquired with variable slice spacing (e.g. a lot of slices near the brain stem and wider spacing for more superior locations). In this case dcm2niix generates a warning and creates an interpolated volume with equal slice spacing that has ‘_Eq’ appended to the file name

Unaligned slices are saved as separate volumes.

Gantry tilt is sometimes seen in CT scans. dcm2niix reports this skew in the NIfTI s-form. However, while the NIfTI format supports sheared volumes using the sform, most tools require rectangular volumes (e.g. ITK-based tools like slicer). Therefore, if gantry tilt is detected, dcm2niix also generates an interpolated volume with ‘_Tilt’ appended to the filename.

Does dcm2niix support CT reconstruction based on different DICOM tags (frame time, heart cycle phase, various vendor-specific tags)?

Most dcm2niix users work with MRI scans, and so it has been most thoroughly tested in that modality If you have CT examples with these features and dcm2niix does not work as expected, you can generate an issue on the dcm2niix GitHub page. A command line option allows you to specify if varying X-ray exposures should be stacked in a single volume or saved as separate volumes, as different users prefer different behavior. In general, different properties (for MRI: echo time, image type, trigger time, etc) are saved as separate files as described below.

Can dcm2niix can load multiple series as a 4D volume?

In general, separate series are saved as separate files, and this is typically desirable (e.g. for fMRI analyses separate runs are typically saved as separate 4D volumes). An exception is DSC, where Siemens uses separate series for successive volumes. The next release (and current developmental release) of dcm2niix stack these DSC series as a single volume.
Scans of the same series with the same properties are saved as a single 4D volume. Therefore, scans like DSC, fMRI, ASL, DWI, etc are saved as 4D volumes. Separate files are saved for different properties (e.g. type: magnitude, real, imaginary and phase; echo time; trigger delay time, etc.). These situations are described in thedcm2niix file naming page.


To be more correct, setting up an environment that packages and tests those tools on a reference sample dataset was the desired goal. Dockers for some of the packages were completed, but the project was never continued after those initial steps.

@Chris_Rorden Thanks a lot for the detailed answer. You’ve done a great job with dcm2niix and nicely implemented all the essential image normalization techniques. We could use your library as a DICOM import plugin in Slicer for some 3D and 4D images, simplifying or replacing current scalar and multivolume plugins. We already have lots of infrastructure developments planned for the near future, but we may get back to this in about a year.

1 Like

A quick but useful initial step could be to provide dcm2niix based DICOM plugin in an extension. I would even avoid superbuilding it, but maybe just pulling a packaged binary from the dcm2niix repo, or letting users install it locally from package, and point to the location of the binary in a setting.


Please see the following to add an extension (no DICOM browser plugin). We will use this as a dependency for a scripted module within SlicerDMRI, but made the dcm2niix build part a separate extension for reusability.

I set it up to build with superbuild, because (a) it is easy to build, and (b) we want it to have the same host runtime requirements as Slicer – for example, it would be confusing for users if the dcm2niix-provided binaries are built on a newer linux host and don’t run on the same platform as the rest of Slicer.