Max Marcus
Max Marcus

Reputation: 106

Pydicom.read_file is only working with some Dicom images

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?

Upvotes: 8

Views: 17126

Answers (2)

deepak sen
deepak sen

Reputation: 507

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.

Upvotes: 2

scaramallion
scaramallion

Reputation: 1330

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

Upvotes: 15

Related Questions