I am making a python application that can convert a .dcm image to .jpg using the pydicom library. This is my code:
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)
I have tested it on two files. The first one,
https://github.com/MaxwellMarcus/Training-Data-Creator/blob/master/testDicom.dcm
worked fine.
The second one,
https://github.com/MaxwellMarcus/Training-Data-Creator/blob/master/testDicom2.dcm
gave an error:
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)
Does anybody know why this is happening on only one of the two files, or how to fix it?