What is in an image file besides the pixels?

An image file holds an array of numbers and a header that says what the numbers mean. We already learned that the image array alone cannot tell a 5 mm nodule from a 10 mm one, or tell a Hounsfield unit from a stored integer, or even tell left from right.

File headers with metadata can answer these questions and more. Geometry: the voxel spacing, the position of the first voxel in the patient, and the orientation of the axes. Values: how many bits are used, whether they are signed, how to turn them into physical units, and whether they were compressed. Provenance: which modality, scanner and protocol produced the image, when, and of whom.

The same pixel values can carry a lot more meaning, depending on the file format. DICOM keeps the geometry, the values, the provenance and the patient's identity, in one file per slice. NIfTI keeps the geometry and a value scaling.

The same pixel values can carry a lot more meaning, depending on the file format. DICOM keeps the geometry, the values, the provenance and the patient's identity, in one file per slice. NIfTI keeps the geometry and a value scaling.

Formats trade completeness for convenience. DICOM keeps all three kinds of information, plus the patient's identity. It is not a wrapper around other files: each DICOM file holds one image with its own header of hundreds of tags, so a CT scan arrives as a folder of hundreds of files (more on this below). That makes DICOM awkward to share, and most deep learning code expects one array per example rather than a folder of files, although some loaders, such as MONAI's, read DICOM directly.

Leaner formats, such as NIfTI (.nii), PNG (.png) and JPEG (.jpg), or arrays saved in NumPy's own format (.npy), hold a volume or image per file and load fast, but keep less of the header. AI pipelines therefore often convert, and keep the metadata they still need in a text or table file next to the images, such as .json, .csv or .tsv; large projects use .parquet tables or a small database. A conversion can keep everything, but it can also drop metadata, change pixel values by rescaling, casting or lossy compression, or reorient the array, so check what the converter did and keep the metadata and provenance you will need.

Common biomedical image formats

The common formats at a glance: where you meet each one, what one file holds, which geometry and units it keeps, how it stores pixels, and what to check after loading.

The common formats at a glance: where you meet each one, what one file holds, which geometry and units it keeps, how it stores pixels, and what to check after loading.

We cover some of the most important formats below. In radiology almost every image starts as a DICOM file, so we start there.

DICOM: a format for image array, geometry, values, and provenance

DICOM is the format that most clinical scanners write and that hospital archives (PACS) store. A DICOM file (.dcm) holds one image, such as one CT slice or one radiograph, together with a header of named tags. A CT scan is therefore not one file but a folder of several hundred. No file wraps them together. Instead, each file's header carries identifiers, generated by the scanner and unique worldwide, that say where the file belongs. All slices of one reconstruction form a series and share a SeriesInstanceUID. All series from one visit to the scanner, for example a CT before and after contrast plus the scout images, form a study and share a StudyInstanceUID. Some newer scanners instead write enhanced multi-frame files: a single file that holds all the frames of a volume, an ultrasound cine loop or a whole slide, with NumberOfFrames saying how many. We write tags by their keyword, the name that pydicom uses (ds.PixelSpacing). The tags that matter for analysis generally fall into four groups:

Sorting the slices of a CT series by file name (middle) scrambles the anatomy; sorting by the z value of  (right) restores it.

Sorting the slices of a CT series by file name (middle) scrambles the anatomy; sorting by the z value of ImagePositionPatient (right) restores it.

To build a volume, collect the files of one series, sort them by their position along the slice axis, and check that neighboring slices are equally spaced (figure). Sorting by file name instead can scramble the anatomy, because nothing makes the names follow it. SimpleITK's series reader and dcm2niix sort for you, but silently: a missing slice or two mixed series comes out wrong without an error.

NIfTI format

NIfTI (.nii, or .nii.gz when compressed) stores a whole volume in one file: a small header followed by the voxel array. It began in brain imaging and is now the usual research format for 3D and 4D medical images in every specialty, from abdominal oncology to cardiology. Researchers convert DICOM to NIfTI because it turns a folder of hundreds of slice files into one file that loads with one line of Python, and because most 3D tools, such as nnU-Net, expect it. Almost everyone converts with dcm2niix, which writes one NIfTI file per DICOM series.