How to Open DICOM Images in Python
This ain’t your daddy’s medical imaging format.
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.
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
- Python
- PyDicom
- Matplotlib (for visualization)
- NumPy (helps Matplotlib handle…