|
ViewingImages
How to use other packages with pydicom to view DICOM images
Phase-Use Introductionpydicom 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 matplotlibmatplotlib 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 TkinterThe 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 |
Note that the PIL example can be done more simply:
The PIL more simply usage raises an error: "TypeError?: Cannot handle this data type"
Hm... works for me. What version of PIL are you running? Is your DICOM just one slice?
I used old version of PIL. It work's with v.1.1.7. Thanks!
The PIL more simply gives a blurred image...
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
>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)
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..
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.