如何修复此代码以显示图像缩略图

问题描述

以下代码显示\data目录中可用的不同图像的缩略图。但是,这有问题。

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()

我遇到以下错误

---------------------------------------------------------------------------
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,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)

我尝试做!conda install -c conda-forge gdcm -y,但遇到以下错误

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.

您知道如何解决错误吗?

解决方法

在下一个pydicom版本中将可用的GDCM替代方法是pylibjpeg(公开:我是pydicom的撰稿人和pylibjpeg的作者)。您只需在调用Dataset.pixel_array之前将其导入即可将pylibjpeg与pydicom v2.0一起使用:

安装

pip install pylibjpeg pylibjpeg-libjpeg

用法

from pydicom import dcmread
import pylibjpeg

ds = dcmread('path/to/file.dcm')
arr = ds.pixel_array
,

如果不是绝对需要python 3.8,则可以尝试使用python 3.7解释器通过使用以下命令行来创建新环境:

conda create -n myenv python=3.7

您还可以尝试从现有环境中使用pip安装依赖项。