Search code examples
arrayspython-3.xpydicom

Issue with pixel_array using pydicom on python 3.x


I'm using pydicom (installed with pip3, on python 3.7, using Idle) and I need to access pixel_array values.

I just copy-paste the example provided into the documentation and this leads to two errors:

  • first is about the get_testdata_files operation, which is not working because

    Python 3.7.3 (v3.7.3:ef4ec6ed12, Mar 25 2019, 22:22:05) [MSC v.1916 64 bit (AMD64)] on win32
    Type "help", "copyright", "credits" or "license()" for more information.
    >>> 
    ==================== RESTART: D:\OneDrive\Desktop\test.py ====================
    None
    Traceback (most recent call last):
      File "D:\OneDrive\Desktop\test.py", line 8, in <module>
        filename = get_testdata_files("bmode.dcm")[0]
    IndexError: list index out of range
    

I have solved this not using this operation.

  • second is about the pixel_array and I'm not so able to decode what is wrong, but it seems like the pixel_array is not populated. However I'm able to access other fields in the dataset and the file can be displayed (using ImageJ for example).

    ==================== RESTART: D:\OneDrive\Desktop\test.py ====================
    None
    
    Filename.........: bmode.dcm
    Storage type.....: 1.2.840.10008.5.1.4.1.1.3.1
    
    Patient's name...: Femoral trombenarterectomy, Case Report:
    Patient id.......: Case Report 1
    Modality.........: US
    Study Date.......: 20110824
    Image size.......: 768 x 1024, 27472108 bytes
    Slice location...: (missing)
    Traceback (most recent call last):
    File "D:\OneDrive\Desktop\test.py", line 38, in <module>
         plt.imshow(dataset.pixel_array, cmap=plt.cm.bone)
    File "C:\Users\marcl\AppData\Local\Programs\Python\Python37\lib\site-packages\pydicom\dataset.py", line 949, in pixel_array
         self.convert_pixel_data()
    File "C:\Users\marcl\AppData\Local\Programs\Python\Python37\lib\site-packages\pydicom\dataset.py", line 895, in convert_pixel_data
         raise last_exception
    File "C:\Users\marcl\AppData\Local\Programs\Python\Python37\lib\site-packages\pydicom\dataset.py", line 863, in convert_pixel_data
    arr = handler.get_pixeldata(self)
     File "C:\Users\marcl\AppData\Local\Programs\Python\Python37\lib\site-packages\pydicom\pixel_data_handlers\pillow_handler.py", line 188, in get_pixeldata
    UncompressedPixelData.extend(decompressed_image.tobytes())
    File "C:\Users\marcl\AppData\Local\Programs\Python\Python37\lib\site-packages\PIL\Image.py", line 746, in tobytes
    self.load()
    File "C:\Users\marcl\AppData\Local\Programs\Python\Python37\lib\site-packages\PIL\ImageFile.py", line 261, in load
    raise_ioerror(err_code)
    File "C:\Users\marcl\AppData\Local\Programs\Python\Python37\lib\site-packages\PIL\ImageFile.py", line 58, in raise_ioerror
    raise IOError(message + " when reading image file")
    

    OSError: broken data stream when reading image file

Here is my code:

import matplotlib.pyplot as plt
import sys
import pydicom
import numpy
from pydicom.data import get_testdata_files

print(__doc__)
#filename = get_testdata_files("bmode.dcm")[0]
filename = "bmode.dcm"
dataset = pydicom.dcmread(filename)

# Normal mode:
print()
print("Filename.........:", filename)
print("Storage type.....:", dataset.SOPClassUID)
print()

pat_name = dataset.PatientName
display_name = pat_name.family_name + ", " + pat_name.given_name
print("Patient's name...:", display_name)
print("Patient id.......:", dataset.PatientID)
print("Modality.........:", dataset.Modality)
print("Study Date.......:", dataset.StudyDate)

if 'PixelData' in dataset:
    rows = int(dataset.Rows)
    cols = int(dataset.Columns)
    print("Image size.......: {rows:d} x {cols:d}, {size:d} bytes".format(
        rows=rows, cols=cols, size=len(dataset.PixelData)))
    if 'PixelSpacing' in dataset:
        print("Pixel spacing....:", dataset.PixelSpacing)


# use .get() if not sure the item exists, and want a default value if missing
print("Slice location...:", dataset.get('SliceLocation', "(missing)"))

# plot the image using matplotlib
plt.imshow(dataset.pixel_array, cmap=plt.cm.bone)
plt.show()

Could you help me to solve these two errors and access pixel_array values? Don't hesitate to give me some advices /remarks/...

Thanks!


Solution

  • Hi Marc welcome to SO!

    Your first error means that the get_testdata_files returns an empty list, so your file is not found. Have a look at the pydicom source, it shows that a search is performed in [DATA_ROOT]/test_files. Is your file located in that path?

    Your second error is related to PIL and that can be quite difficult to debug and fix. First try to read the pixel_array from a dataset created from one of the supplied test files. If that works, your problem is probably that PIL cannot handle the specific encoding of your image data. You want to install and use GDCM instead of PIL to see if that solves the problem. Another user has had a similar issue as you, GDCM solved the problem. It can be a bit of a headache to get working unfortunately. Or have a look at this page, it shows some other alternatives on viewing the image data.