之前采用pydicom读取dicom文件一切都很正常,不过最近读取一批数据的时候,会报错
读取代码
file = pydicom.read_file(filepath) data = file.pixel_array
问题就出在pixel_array这个属性上,报错如下
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
看到一篇博客https://blog.csdn.net/inter_peng/article/details/74370306与我的问题类似,都出在dicom文件格式上面,这种压缩格式用pydicom读取不出来,采用SimpleITK可以解决
file = sitk.ReadImage(filepath) data = sitk.GetArrayFromImage(file)
来源:https://www.cnblogs.com/wzyuan/p/10489499.html