My favorites | Sign in
Project Home Downloads Wiki Issues Source
Search
for
ViewingImages  
How to use other packages with pydicom to view DICOM images
Phase-Use
Updated Jan 3, 2011 by darcymason@gmail.com

Introduction

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:

Using pydicom with matplotlib

matplotlib is available at http://matplotlib.sourceforge.net/. It can take 2-d image information from Dataset.pixel_array and display it. Here is an example:

>>> import dicom
>>> import pylab
>>> ds=dicom.read_file("CT_small.dcm")
>>> pylab.imshow(ds.pixel_array, cmap=pylab.cm.bone)
<matplotlib.image.AxesImage object at 0x0162A530>
>>> pylab.show()
>>>

Thanks to Roy Keyes for pointing out how to do this.

Using pydicom with Python Imaging Library (PIL)

See the example file in the contrib directory: http://code.google.com/p/pydicom/source/browse/source/dicom/contrib/pydicom_PIL.py

The key line is as follows, but the example file shows how to also adjust the window and level if available

im = Image.frombuffer(mode, size, ds.PixelData, "raw", mode, 0, 1)
im.show()

Using pydicom with Tkinter

The following contrib file shows a method for viewing images using Tkinter: http://code.google.com/p/pydicom/source/browse/source/dicom/contrib/pydicom_Tkinter.py

Comment by njv...@gmail.com, Dec 15, 2010

Note that the PIL example can be done more simply:

im = Image.fromarray(ds.pixel_array)
im.show()
Comment by s...@mail.by, Dec 27, 2010

The PIL more simply usage raises an error: "TypeError?: Cannot handle this data type"

Comment by njv...@gmail.com, Jan 14, 2011

Hm... works for me. What version of PIL are you running? Is your DICOM just one slice?

Comment by V.Paramo...@gmail.com, Jan 25, 2011

I used old version of PIL. It work's with v.1.1.7. Thanks!

Comment by asrit...@gmail.com, Jan 26, 2011

The PIL more simply gives a blurred image...

Comment by jmzavala...@gmail.com, Jan 22, 2012

hi, when i work with file JPEG2000.dcm an others, pydicom send

NotImplementedError?: Pixel Data is compressed in a format pydicom does not yet handle. Cannot return array

Comment by project member darcymason@gmail.com, Jan 22, 2012

>when i work with file JPEG2000.dcm an others, pydicom send >NotImplementedError??: Pixel Data is compressed in a format pydicom does not yet handle. Cannot return array

As noted on the main page, pydicom does not decode compressed pixel data. It is very complex to do in a pure-python package, although hopefully one day it will be implemented. A work-around is to first uncompress the images using another toolkit. For example, dcmtk has command line tools for this. (e.g. http://support.dcmtk.org/docs/dcmdjpeg.html)

Comment by duranr2002, Mar 29, 2012

hi, i work in Windows 7 32-bit, Python 2.7, pydicom 0.9.7 and PIL 1.1.7

i tried to viewing DCM file. but i can't.

Python console messege is below.

Traceback (most recent call last):
  File "<pyshell#9>", line 1, in <module>
    show_PIL(ds)
  File "C:\Python27\lib\site-packages\dicom\contrib\pydicom_PIL.py", line 86, in show_PIL
    image = get_LUT_value(dataset.pixel_array, dataset.WindowWidth, dataset.WindowCenter)
  File "C:\Python27\lib\site-packages\dicom\contrib\pydicom_PIL.py", line 58, in get_LUT_value
    [data <= (level - 0.5 - (window-1)/2),
TypeError: unsupported operand type(s) for -: 'DS' and 'float'

how can i handle this? thx..

Comment by project member darcymason@gmail.com, Mar 29, 2012

The TypeError? is as a result of changes in pydicom 0.9.7 to how data elements of type 'DS' are handled -- as a python Decimal. python does not automatically convert numeric types. The solution is to wrap the DS data elements with float(), e.g. float(level) in the last line, and any others that are needed.

Comment by wangxiao...@gmail.com, Apr 25, 2012
Note that the PIL 1.1.7, in Image.py line 1910, add ((1, 1), "<u2"): ("I", "I;16")

Sign in to add a comment
Powered by Google Project Hosting