Tag Archives: Pydicom Read Dicom File Error

How to Solve Pydicom Read Dicom File Error: OSError

Before using pydicom to read dicom files, everything is normal, but when reading a batch of data recently, an error will be reported

Read code

file = pydicom.read_file(filepath)
data = file.pixel_array

The problem lies in the attribute of pixel_array, the error is reported as follows

OSError                                   Traceback (most recent call last)
c:\python35\lib\site-packages\pydicom\pixel_data_handlers\pillow_handler.py in get_pixeldata(dicom_dataset)
    196                 fio = io.BytesIO(pixel_data)
--> 197                 decompressed_image = Image.open(fio)
    198             except IOError as e:

c:\python35\lib\site-packages\PIL\Image.py in open(fp, mode)
   2571     raise IOError("cannot identify image file %r"
-> 2572                   % (filename if filename else fp))
   2573 

OSError: cannot identify image file <_io.BytesIO object at 0x000002418FC85CA8>

During handling of the above exception, another exception occurred:

NotImplementedError                       Traceback (most recent call last)
<ipython-input-55-c00f3f09682d> in <module>()
----> 1 file.pixel_array

c:\python35\lib\site-packages\pydicom\dataset.py in pixel_array(self)
    899             The Pixel Data (7FE0,0010) as a NumPy ndarray.
    900         """
--> 901         self.convert_pixel_data()
    902         return self._pixel_array
    903 

c:\python35\lib\site-packages\pydicom\dataset.py in convert_pixel_data(self)
    845         )
    846 
--> 847         raise last_exception
    848 
    849     def decompress(self):

c:\python35\lib\site-packages\pydicom\dataset.py in convert_pixel_data(self)
    813             try:
    814                 # Use the handler to get a 1D numpy array of the pixel data
--> 815                 arr = handler.get_pixeldata(self)
    816                 self._pixel_array = reshape_pixel_array(self, arr)
    817 

c:\python35\lib\site-packages\pydicom\pixel_data_handlers\pillow_handler.py in get_pixeldata(dicom_dataset)
    197                 decompressed_image = Image.open(fio)
    198             except IOError as e:
--> 199                 raise NotImplementedError(e.strerror)
    200             UncompressedPixelData.extend(decompressed_image.tobytes())
    201     except Exception:

NotImplementedError: None

 

Solution:

this compression format can not be read with pydicom, using SimpleITK can solve

file = sitk.ReadImage (filepath) 
data = sitk.GetArrayFromImage (file)