Skip to content

Instantly share code, notes, and snippets.

@yobayob
Created July 31, 2017 06:25
Show Gist options
  • Save yobayob/8ebc0ab6ccfcf5c1fea5a3af4170dd95 to your computer and use it in GitHub Desktop.
Save yobayob/8ebc0ab6ccfcf5c1fea5a3af4170dd95 to your computer and use it in GitHub Desktop.
import matplotlib.pyplot as plt
import dicom
import os
dicomdir = "~/Downloads/test/CDROM/DICOMDIR"
ds = dicom.read_dicomdir(dicomdir)
pixel_data = list()
for record in ds.DirectoryRecordSequence:
if record.DirectoryRecordType == "IMAGE":
# Extract the relative path to the DICOM file
path = os.path.join(os.path.dirname(dicomdir), *record.ReferencedFileID)
dcm = dicom.read_file(path)
# Now get your image data
pixel_data.append(dcm.pixel_array)
for ds in pixel_data:
plt.imshow(ds, cmap=plt.cm.bone)
plt.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment