35  Python Libraries for Radiology Image Format Conversion

There are several excellent Python libraries for this. Here’s an overview:

35.1 Format Conversion Matrix

                   ┌─────────────────────────────────────────────┐
                   │              TARGET FORMAT                  │
                   ├──────────┬──────────┬──────────┬────────────┤
                   │  DICOM   │  NIfTI   │   NRRD   │  PNG/JPEG  │
┌───────┬──────────┼──────────┼──────────┼──────────┼────────────┤
│       │  DICOM   │    -     │ SimpleITK│ SimpleITK│  pydicom + │
│ S     │          │          │ dcm2niix │          │  Pillow    │
│ O     ├──────────┼──────────┼──────────┼──────────┼────────────┤
│ U     │  NIfTI   │ SimpleITK│    -     │ SimpleITK│  nibabel + │
│ R     │          │          │          │          │  Pillow    │
│ C     ├──────────┼──────────┼──────────┼──────────┼────────────┤
│ E     │   NRRD   │ SimpleITK│ SimpleITK│    -     │  pynrrd +  │
│       │          │          │          │          │  Pillow    │
│       ├──────────┼──────────┼──────────┼──────────┼────────────┤
│       │ PNG/JPEG │ pydicom  │ nibabel  │  pynrrd  │     -      │
└───────┴──────────┴──────────┴──────────┴──────────┴────────────┘

35.2 Core Libraries

SimpleITK is probably the most versatile single library for your needs. It can read and write DICOM, NRRD, NIfTI (.nii, .nii.gz), and common formats—all through a unified API.

import SimpleITK as sitk

# Read any supported format
image = sitk.ReadImage("input.nrrd")

# Write to another format
sitk.WriteImage(image, "output.nii.gz")

pydicom is the go-to for DICOM-specific operations (reading metadata, modifying tags, etc.), but it doesn’t natively write to NIfTI or NRRD.

nibabel specializes in neuroimaging formats (NIfTI, MGH, ANALYZE) and is commonly used in brain imaging pipelines.

pynrrd is a lightweight library specifically for NRRD files.

35.4 Special Mention: dcm2niix

For DICOM → NIfTI specifically (especially multi-slice series), dcm2niix is the gold standard CLI tool. You can call it from Python:

import subprocess
subprocess.run(["dcm2niix", "-o", "output_dir", "dicom_folder"])