8

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?

Max Marcus
  • 106
  • 1
  • 1
  • 4

2 Answers2

14

You have a dataset that contains compressed Pixel Data. By itself pydicom can only handle Pixel Data that hasn't been compressed, but if you install one or more optional libraries then it can handle various compressions. This table tells you which package is required.

For JPEG Lossless, Non-hierarchical, 1st Order Prediction (1.2.840.10008.1.2.4.70), the only listed package available is GDCM, which unfortunately means you either require a Conda install (and on conda-forge its only available on Windows up to Python 3.6) or linux. Fortunately, there's an new package that I've been working on that also supports JPEG Lossless: pylibjpeg with the -libjpeg plugin.

$ pip install pylibjpeg pylibjpeg-libjpeg pydicom
from pydicom import dcmread
import pylibjpeg

ds = dcmread("testDicom2.dcm")
arr = ds.pixel_array

Looking at your data, I'd say the Photometric Interpretation value is wrong, too. Change it to YBR_FULL first:

from pydicom import dcmread
import pylibjpeg

ds = dcmread("testDicom2.dcm")
ds.PhotometricInterpretation = 'YBR_FULL'
arr = ds.pixel_array
scaramallion
  • 1,251
  • 1
  • 6
  • 14
  • Hi, thanks for the answer. However, I am still facing the same error after following your suggestions. Any clue to fix this? – kenneth May 10 '21 at 14:01
  • 1
    You could try `pip install python-gdcm`, which is a new project that builds the Python wrappers for GDCM and should have wheels available on Windows up to Python 3.9 – scaramallion May 10 '21 at 20:48
  • Hi, thank you for the answer. I had installed python-gdcm and pylibjpeg, however the error is still there. Have been looking around for the solution but still no luck (I am using python 3.7) – kenneth May 11 '21 at 03:14
  • You don't have a `gdcm` folder in your working directory by any chance? – scaramallion May 11 '21 at 03:18
  • Hi, the installed gdcm is in my installation directory, but not the working directory where I store/read those DICOM files. – kenneth May 11 '21 at 03:30
  • I'd try updating everything (pydicom, pylibjpeg, pylibjpeg-libjpeg, pylibjpeg-openjpeg), and if the issue still occurs create a new [issue](https://github.com/pydicom/pydicom/issues/new/choose)? – scaramallion May 11 '21 at 04:16
2

Second image is compressed dicom image and pydicom uses different packages such as GDCM and Pillow(for jpg, jpeg) for processing these images.

Installing one of the packages would solve the problem.

deepak sen
  • 437
  • 4
  • 13