2

i am trying to read CT scan Dicom file using pydicom python library but i just can't get rid of this below error even when i install gdcm and pylibjpeg

RuntimeError: The following handlers are available to decode the pixel data however they are missing required dependencies: GDCM (req. ), pylibjpeg (req. )

Here is my code

!pip install pylibjpeg pylibjpeg-libjpeg pylibjpeg-openjpeg
!pip install python-gdcm

import gdcm
import pylibjpeg

import numpy as np
import pydicom
from pydicom.pixel_data_handlers.util import apply_voi_lut

import matplotlib.pyplot as plt
%matplotlib inline


def read_xray(path, voi_lut = True, fix_monochrome = True):
    dicom = pydicom.read_file(path)
    
    # VOI LUT (if available by DICOM device) is used to transform raw DICOM data to "human-friendly" view
    if voi_lut:
        data = apply_voi_lut(dicom.pixel_array, dicom)
    else:
        data = dicom.pixel_array
               
    # depending on this value, X-ray may look inverted - fix that:
    if fix_monochrome and dicom.PhotometricInterpretation == "MONOCHROME1":
        data = np.amax(data) - data
        
    data = data - np.min(data)
    data = data / np.max(data)
    data = (data * 255).astype(np.uint8)
        
    return data

img = read_xray('/content/ActiveTB/2018/09/17/1.2.392.200036.9116.2.6.1.44063.1797735841.1537157438.869027/1.2.392.200036.9116.2.6.1.44063.1797735841.1537157440.863887/1.2.392.200036.9116.2.6.1.44063.1797735841.1537154539.142332.dcm')
plt.figure(figsize = (12,12))
plt.imshow(img)

Here is the image link on which i am trying to run this code

https://drive.google.com/file/d/1-xuryA5VlglaumU2HHV7-p6Yhgd6AaCC/view?usp=sharing
  • 1
    Looks like you need to install `GDCM` and `pylibjpeg` – The Singularity Sep 07 '21 at 10:35
  • 2
    Actually you need only one of them. Check the documentation for installing [pylibjpeg](https://pydicom.github.io/pydicom/stable/tutorials/installation.html#installing-pylibjpeg) or [GDCM](https://pydicom.github.io/pydicom/stable/tutorials/installation.html#installing-gdcm). – MrBean Bremen Sep 07 '21 at 10:53

2 Answers2

1

Try running the following:

!pip install pylibjpeg
!pip install gdcm
The Singularity
  • 2,428
  • 3
  • 19
  • 48
  • 1
    That is not entirely correct - see the documentation links in my comment above. – MrBean Bremen Sep 07 '21 at 11:32
  • 1
    Hi, Thankyou for taking this question, I tried luke way also but the error still persist, I got rid of the error in conda. I did the same thing what I was doing in Google colab and pycharm. At last conda solve my problem, but I don't know the reason why it worked on conda and fail on colab and pycharm. – Nimesh Kumar Sep 08 '21 at 11:03
  • 1
    No, That runtime error was the only error that I was getting while converting. i did the same thing in conda and it was up and running. – Nimesh Kumar Sep 10 '21 at 07:57
0

In a similar problem as yours, we could see that the problem persists at the Pixel Data level. You need to install one or more optional libraries, so that you can handle the various compressions.

First, you should do:

$ pip uninstall pycocotools
$ pip install pycocotools --no-binary :all: --no-build-isolation

From here, you should do as follows:

$ pip install pylibjpeg pylibjpeg-libjpeg pydicom

Then, your code should look like this:

from pydicom import dcmread
import pylibjpeg
import gdcm

import numpy as np
import pydicom
from pydicom.pixel_data_handlers.util import apply_voi_lut

import matplotlib.pyplot as plt
%matplotlib inline


def read_xray(path, voi_lut = True, fix_monochrome = True):
    dicom = dcmread(path)
    
    # VOI LUT (if available by DICOM device) is used to transform raw DICOM data to "human-friendly" view
    if voi_lut:
        data = apply_voi_lut(dicom.pixel_array, dicom)
    else:
        data = dicom.pixel_array
               
    # depending on this value, X-ray may look inverted - fix that:
    if fix_monochrome and dicom.PhotometricInterpretation == "MONOCHROME1":
        data = np.amax(data) - data
        
    data = data - np.min(data)
    data = data / np.max(data)
    data = (data * 255).astype(np.uint8)
        
    return data

img = read_xray('/content/ActiveTB/2018/09/17/1.2.392.200036.9116.2.6.1.44063.1797735841.1537157438.869027/1.2.392.200036.9116.2.6.1.44063.1797735841.1537157440.863887/1.2.392.200036.9116.2.6.1.44063.1797735841.1537154539.142332.dcm')
plt.figure(figsize = (12,12))
plt.imshow(img)
Francisco Maria Calisto
  • 2,841
  • 4
  • 22
  • 54