DICOM to NIfTI conversion is a series extract: point dcm2niix at a DICOM folder, get one .nii / .nii.gz plus a BIDS JSON sidecar. The same job in Python is dicom2nifti.dicom_series_to_nifti. This page is that how-to. It is not NIfTI → DICOM, and it is not a DICOM-vs-NIfTI explainer.
If you meant wrap a NumPy array as NIfTI → how to convert an array into NIfTI. If you meant NIfTI → a DICOM series → NIfTI to DICOM in Python. If you meant write extra header fields → store metadata in NIfTI and NRRD. If you meant NIfTI → STL → convert NIfTI to STL. If you meant DICOM → JPEG/PNG → how to convert a DICOM image into JPG or PNG. If you meant what DICOM is → what is DICOM.
Why convert at all
DICOM is the clinical record: pixels plus patient, study, and scanner tags, one file per slice (or one multi-frame object). NIfTI is the neuroimaging extract: one volume, a 4×4 affine, and a small header. Analysis tools (SPM, FSL, most Python pipelines) want the extract. The conversion is that extract — not a format debate.
Tools that actually convert a series
| Tool | How you run it | BIDS sidecar | Best for |
|---|---|---|---|
| dcm2niix | CLI (dcm2niix <folder>) |
Yes (JSON) | Speed, transfer-syntax coverage, reproducible pipelines |
| MRIConvert | GUI | Limited | A one-off folder when you do not want a terminal |
| SPM DICOM Import | Inside SPM | Limited | You already live in SPM and do not want a second tool |
This page teaches dcm2niix and the dicom2nifti Python call. MRIConvert and SPM are named so you can pick them; they are not a second tutorial.
Install dcm2niix
Windows: download the zip from the dcm2niix releases, unpack it, add that folder to PATH so the binary runs from any prompt.
macOS:
brew install dcm2niix
Debian / Ubuntu:
sudo apt-get install dcm2niix
Sanity check: dcm2niix -h prints the help. Docs: rordenlab/dcm2niix.
One subject folder
dcm2niix /path/to/DICOM_directory
dcm2niix walks the folder, groups slices into series, writes one NIfTI per series, and writes a JSON sidecar next to each volume. Put the output somewhere else with -o:
dcm2niix -o /path/to/nifti_out /path/to/DICOM_directory
Use .nii.gz unless a downstream tool insists on uncompressed .nii.
Many subjects: a bash loop
for subject in subject_01 subject_02 subject_03; do
mkdir -p output_directory/$subject
dcm2niix -o output_directory/$subject subject_data/$subject
done
Each subject folder stays a series convert. The loop is organization, not a different algorithm. Multi-sequence studies (T1, T2, fMRI) become separate NIfTI files; the sidecar names the sequence.
Check the output
Open the NIfTI in MRIcroGL (or ITK-SNAP / 3D Slicer). Confirm slice count, dimensions, and that left/right match the DICOM. A flipped volume is an orientation bug, not “close enough.” dcm2niix’s -x flag is the usual lever when a series comes out mirrored; re-run and look again before you train on it.
Python: dicom2nifti
When the convert has to live inside a script, use dicom2nifti.
python -m venv dicom_nifti
source dicom_nifti/bin/activate # Windows: dicom_niftiScriptsactivate
pip install dicom2nifti
import dicom2nifti
dicom_directory = "path/to/dicom_series"
output_file = "path/to/output/scan.nii.gz"
dicom2nifti.dicom_series_to_nifti(dicom_directory, output_file, reorient_nifti=True)
dicom_series_to_nifti wants the folder of one series and a full output path, extension included. reorient_nifti=True puts the volume into a RAS-like orientation most Python stacks expect.
A folder of patients:
from pathlib import Path
import dicom2nifti
input_root = Path("path/to/patients")
output_root = Path("path/to/nifti_output")
output_root.mkdir(parents=True, exist_ok=True)
for dicom_directory in input_root.iterdir():
if not dicom_directory.is_dir():
continue
output_file = output_root / f"{dicom_directory.name}.nii.gz"
dicom2nifti.dicom_series_to_nifti(str(dicom_directory), str(output_file), reorient_nifti=True)
Same check as the CLI: open a few outputs and confirm anatomy and count before you batch the rest.
What usually breaks
Inconsistent headers. Vendors fill the same tag differently. dcm2niix is built for that range of transfer syntaxes; a GUI converter often is not.
Proprietary sequences. Some manufacturer sequences are not vanilla DICOM. You may need the vendor tool, or a script another lab already wrote for that sequence. The CLI warning is the clue — do not ignore it and hope the volume is fine.
Orientation. DICOM patient space and the NIfTI affine are different conventions. Wrong conversion = a mirrored or rotated brain. Use -x when you must flip; always verify in a viewer.
Multi-frame DICOM. Several frames in one file. dcm2niix splits them and keeps timing in the sidecar. A tool that treats the object as one slice will drop the rest.
The BIDS sidecar is the metadata you keep
NIfTI does not carry DICOM patient tags. That strip is why people convert for research. What you do keep is acquisition context: TR, TE, slice timing, echo train, diffusion directions, the affine. dcm2niix writes those into a JSON sidecar next to the .nii.gz. Name the pair the BIDS way (sub-01_T1w.nii.gz + sub-01_T1w.json) and FSL / SPM / most BIDS apps will find the JSON without extra work.
If you need to write extra fields back into the NIfTI header or an NRRD — a few keys you choose, not a patient dump — that is store metadata in NIfTI and NRRD, not this convert.
After a convert, spot-check the sidecar against the DICOM: TR / slice timing on fMRI, b-values and directions on diffusion, pixel spacing and orientation on anything spatial. A volume that “looks right” with a wrong TR is still a bad convert.
PYCAD builds the imaging side of products that have to run this convert for real. Case studies.