How to Open DICOM Images in Python

Proto Bioengineering
4 min readJul 2, 2024

This ain’t your daddy’s medical imaging format.

Photo by National Cancer Institute on Unsplash

DICOM files (.dcm) are the most popular filetype for medical images. DICOM is an international standard that allows doctors to share, store, and analyze medical images and use them in their radiology-viewing software. These medical images can include X-rays, MRIs, CT scans, ultrasound and more.

These are MRIs of a human brain, which are often stored in DICOM format. (From KieranMaher on Wikibooks)

If we want to do anything cool like brain MRI analysis or finding disease with AI, we have to open the image first! Let’s explore how to do this with Python and PyDicom.

All of this code is also available online in our Kaggle notebook.

The Code

Here is a TLDR of the code. Read more below for an in-depth explanation of the code.

import matplotlib.pyplot as plt
import pydicom

# Open the DICOM file
dicom_file = pydicom.dcmread("my_medical_image.dcm")
image = dicom_file.pixel_array

# Show the image
plt.imshow(image)
plt.show()

Requirements

--

--

Proto Bioengineering

Learn to code for science. “Everything simple is false. Everything complex is unusable.” — Paul Valery