How to fix this code to display images thumbnails

383 views Asked by At

The following code is supposed to display thumbnails of different images that are available in the \data directory. However, something is wrong with it.

from os import listdir
from os.path import isfile, join
mypath = "data/"
file_names = [mypath+f for f in listdir(mypath) if isfile(join(mypath, f))]
number_files = len(file_names)
fig = plt.figure(figsize = (40,50)) 
fig.subplots()
axes = []
for i ,file_name in  enumerate(file_names):
    dataset = pydicom.dcmread(file_name)
    axes.append(fig.add_subplot(int(math.sqrt(number_files))+1 , int(math.sqrt(number_files))+1, i+1))
    plt.imshow(dataset.pixel_array, cmap=plt.cm.bone)
plt.show()

I have the following error:

---------------------------------------------------------------------------
RuntimeError                              Traceback (most recent call last)
<ipython-input-73-3ed68a41a4d3> in <module>
     10     dataset = pydicom.dcmread(file_name)
     11     axes.append(fig.add_subplot(int(math.sqrt(number_files))+1 , int(math.sqrt(number_files))+1, i+1))
---> 12     plt.imshow(dataset.pixel_array, cmap=plt.cm.bone)
     13 plt.show()

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

I've tried to do !conda install -c conda-forge gdcm -y but I've gotthe following error:

Collecting package metadata (current_repodata.json): done
Solving environment: failed with initial frozen solve. Retrying with flexible solve.
Solving environment: failed with repodata from current_repodata.json, will retry with next repodata source.
Collecting package metadata (repodata.json): done
Solving environment: failed with initial frozen solve. Retrying with flexible solve.
Solving environment: - 
Found conflicts! Looking for incompatible packages.
This can take several minutes.  Press CTRL-C to abort.
                                                                               failed

UnsatisfiableError: The following specifications were found
to be incompatible with the existing python installation in your environment:

Specifications:

  - gdcm -> python[version='2.7.*|3.5.*|3.6.*|>=2.7,<2.8.0a0|>=3.6,<3.7.0a0|>=3.7,<3.8.0a0|3.4.*']

Your python: python=3.8

If python is on the left-most side of the chain, that's the version you've asked for.
When python appears to the right, that indicates that the thing on the left is somehow
not available for the python version you are constrained to. Note that conda will not
change your python version to a different minor version unless you explicitly specify
that.

Any idea how to fix the error?

2

There are 2 answers

0
scaramallion On

One alternative to GDCM that'll be available in the next released version of pydicom is pylibjpeg (disclosure: I'm a contributor to pydicom and author of pylibjpeg). You can use pylibjpeg with pydicom v2.0 simply by importing it before calling Dataset.pixel_array:

Installation

pip install pylibjpeg pylibjpeg-libjpeg

Usage

from pydicom import dcmread
import pylibjpeg

ds = dcmread('path/to/file.dcm')
arr = ds.pixel_array
0
Treizh On

If don't absolutely need python 3.8, you could try to create a new environment with a python 3.7 interpreter by using the following command line:

conda create -n myenv python=3.7

You could also try to install the dependency with pip from your existing environment.