How to use other packages with pydicom to view DICOM images
Pydicom is mainly concerned with getting at the DICOM data elements in files, but it is often desirable to view pixel data as an image. There are several options:
Use any of the many DICOM viewer programs available
use pydicom with matplotlib
use pydicom with Python’s stdlib Tkinter module.
use pydicom with the Python Imaging Library (PIL)
use pydicom with wxPython
Using pydicom with matplotlib¶
Matplotlib is available at https://matplotlib.org/. It
can take 2D image information from
Dataset.pixel_array and display it.
Here is an example:
>>> import matplotlib.pyplot as plt >>> import pydicom >>> from pydicom.data import get_testdata_files >>> filename = get_testdata_files("CT_small.dcm") >>> ds = pydicom.dcmread(filename) >>> plt.imshow(ds.pixel_array, cmap=plt.cm.bone) <matplotlib.image.AxesImage object at ...>
Thanks to Roy Keyes for pointing out how to do this.
Using pydicom with Tkinter¶
The program pydicom_Tkinter.py
contrib-pydicom repository demonstrates how to show an image using the
Tkinter graphics system, which comes by default with most Python installations.
It creates a Tkinter PhotoImage in a Label widget or a user-supplied widget.
Using pydicom with Python Imaging Library (PIL)¶
The module pydicom_PIL.py
contrib-pydicom repository uses PIL’s
Image.show() method after
creating an Image instance from the pixel data and some basic information
about it (bit depth, LUTs, etc).
Using pydicom with wxPython¶
The module imViewer-Simple.py
contrib-pydicom repository uses wxPython (also PIL, but it notes that it
may not be strictly necessary) to display an image from a pydicom dataset.