Converting NIfTI to DICOM: A Step-by-Step GuideConverting NIfTI (Neuroimaging Informatics Technology Initiative) files to DICOM (Digital Imaging and Communications in Medicine) is a common task when moving neuroimaging data from research environments into clinical workflows or when preparing datasets for systems that require DICOM format. This guide walks through the reasons for conversion, core concepts to understand before converting, several practical methods (command-line tools, Python scripts, and GUI tools), important metadata considerations, QA steps, and tips for automating and integrating conversion into pipelines.
Why convert NIfTI to DICOM?
- Interoperability: Many clinical systems, PACS (Picture Archiving and Communication Systems), and radiology workflows require DICOM.
- Metadata requirements: DICOM encodes patient and study metadata in a standardized way needed by clinical systems.
- Compliance and archiving: DICOM supports long-term archival standards used in hospitals and clinical trials.
- Visualization and tools: Some viewers and PACS tools require or work better with DICOM inputs.
Key differences between NIfTI and DICOM
- File model:
- NIfTI: Typically a single file (or pair .nii/.hdr+.img) representing an entire 3D/4D volume and minimal metadata in a header.
- DICOM: Series of files where each 2D slice is generally a separate file; extensive standardized metadata stored in DICOM tags.
- Metadata richness:
- NIfTI contains voxel dimensions, affine transform, and a few header fields.
- DICOM contains detailed patient, study, series, acquisition, and device metadata.
- Use cases:
- NIfTI: Research, analysis pipelines (FSL, SPM, AFNI).
- DICOM: Clinical imaging, PACS, regulatory requirements.
Preparations: what you need before converting
- Source NIfTI files (.nii or .nii.gz). For 4D datasets (e.g., fMRI), decide whether to keep as 4D or split into 3D volumes.
- Patient/study metadata to populate DICOM tags: patient name, ID, birthdate, sex, study description, accession number, study date/time, referring physician, institution name, and any acquisition-related fields (modality, series description, manufacturer).
- A mapping plan for spatial orientation and affine transforms: NIfTI uses an affine matrix; DICOM uses Image Position (Patient), Image Orientation (Patient), and Pixel Spacing—these must be consistent.
- Tools installed (examples below): dcm2niix (for reverse conversion), nibabel (Python), pydicom, dicom-nifti converters like MRIConvert, heudiconv, or commercial solutions.
Methods
Below are common, practical methods for converting NIfTI to DICOM. Choose according to scale, automation needs, and available tooling.
1) Using dcm2niix-related reverse workflows / MRIConvert / MRIconvert-like tools
Note: dcm2niix itself is primarily DICOM→NIfTI. For reverse conversion, some projects use MRIConvert or specialized scripts. MRIConvert (older) and other GUI tools can repackage NIfTI to DICOM, but may be less flexible for large automated pipelines.
Pros:
- GUI options for one-off conversions.
- Preserves image geometry when supported.
Cons:
- May require manual metadata entry.
- Not ideal for large batch jobs.
2) Using Python: nibabel + pydicom (recommended for automation)
This is the most flexible approach for precise control over metadata, orientation, and integration into pipelines.
High-level steps:
- Load NIfTI with nibabel.
- Extract image data and affine; determine slice order and orientation.
- Create DICOM metadata template (use an existing DICOM as a template when possible).
- For each slice, create a pydicom Dataset, populate required tags, set PixelData, and save as .dcm.
Example Python script (concise, focuses on key steps — adapt metadata as needed):
#!/usr/bin/env python3 # nifti_to_dicom.py import os import uuid import numpy as np import nibabel as nib import pydicom from pydicom.dataset import Dataset, FileDataset from datetime import datetime def nifti_to_dicom(nifti_path, out_dir, template_dcm=None, patient_name="Anon", patient_id="0000", study_description="Converted from NIfTI"): img = nib.load(nifti_path) data = img.get_fdata() affine = img.affine os.makedirs(out_dir, exist_ok=True) # If 4D, handle each volume as a separate series (simple approach) if data.ndim == 4: vols = data.shape[3] else: vols = 1 data = data[..., np.newaxis] for vol in range(vols): series_uid = pydicom.uid.generate_uid() for z in range(data.shape[2]): slice_pixels = np.flipud(data[:, :, z, vol]).astype(np.int16) # Create FileDataset filename = os.path.join(out_dir, f"vol{vol:03d}_slice{z:03d}.dcm") file_meta = Dataset() file_meta.MediaStorageSOPClassUID = pydicom.uid.CTImageStorage file_meta.MediaStorageSOPInstanceUID = pydicom.uid.generate_uid() file_meta.ImplementationClassUID = pydicom.uid.PYDICOM_IMPLEMENTATION_UID ds = FileDataset(filename, {}, file_meta=file_meta, preamble=b"