我正在制作一个 python 应用程序,可以使用 pydicom 库将 .dcm 图像转换为 .jpg。这是我的代码:
import pydicom
import cv2
import numpy as np
filename = 'testDicom.dcm'
#get pixel data from image
ds = pydicom.read_file(filename, force=True)
image = ds.pixel_array
new_img = []
max_value = None
min_value = None
#get maximum and minimum pixel values
for i in image:
for l in i:
if max_value:
if l > max_value:
max_value = l
else:
max_value = l
if min_value:
if l < min_value:
min_value = l
else:
min_value = l
#use maximum and minimum pixel values to map pixel values between 0 and 255
for i in image:
row = []
for pixel in i:
row.append((pixel - min_value) / (max_value / 255.0))
new_img.append(row)
#convert to numpy array
new_img = np.array(new_img)
#save image
cv2.imwrite(filename.replace('.dcm', '.jpg'), new_img)
我已经在两个文件上进行了测试。第一个,
https://github.com/MaxwellMarcus/Training-Data-Creator/blob/master/testDicom.dcm
工作得很好。
第二个,
https://github.com/MaxwellMarcus/Training-Data-Creator/blob/master/testDicom2.dcm
给出了一个错误:
Traceback (most recent call last):
File "C:\Users\Max Marcus\github\Training-Data-Creator\creator.py", line 6, in <module>
image = ds.pixel_array
File "C:\Python38\lib\site-packages\pydicom\dataset.py", line 1615, in pixel_array
self.convert_pixel_data()
File "C:\Python38\lib\site-packages\pydicom\dataset.py", line 1324, in convert_pixel_data
self._convert_pixel_data_without_handler()
File "C:\Python38\lib\site-packages\pydicom\dataset.py", line 1409, in _convert_pixel_data_without_handler
raise RuntimeError(msg + ', '.join(pkg_msg))
RuntimeError: The following handlers are available to decode the pixel data however they are missing required dependencies: GDCM (req. GDCM)
有谁知道为什么只有两个文件之一发生这种情况,或者如何修复它?