【发布时间】:2020-08-18 07:14:28
【问题描述】:
在给定路径上确实存在的 DICOM 文件上,dcmread() 出现问题。我不确定出了什么问题。这是我的代码:
import pydicom
f = "/exact/path/hi.dcm"
fn = get_testdata_file(f)
ds = pydicom.dcmread(fn)
ds.SOPClassUID = ds.MediaStorageSOPClassUID
我遇到以下错误:
Traceback (most recent call last):
File "addfields.py", line 14, in <module>
ds = pydicom.dcmread(fn)
File "/Users/user/opt/anaconda3/lib/python3.7/site-packages/pydicom/filereader.py", line 871, in dcmread
force=force, specific_tags=specific_tags)
File "/Users/user/opt/anaconda3/lib/python3.7/site-packages/pydicom/filereader.py", line 668, in read_partial
preamble = read_preamble(fileobj, force)
File "/Users/user/opt/anaconda3/lib/python3.7/site-packages/pydicom/filereader.py", line 605, in read_preamble
preamble = fp.read(128)
AttributeError: 'NoneType' object has no attribute 'read'
但我不知道为什么,因为“hi.dcm”是一个存在的文件(路径正确)并且里面有元数据。我不知道什么是序言以及它是否出现在我正在处理的图像中。
【问题讨论】:
-
一些工具包不处理没有前导的实例;不确定 pydicom 是否是其中之一。即使存在元数据,前导也可能不存在。许多查看器和转储实用程序也会在没有前导码的情况下正确加载数据集;这并不意味着一切都很好。进一步阅读here。
-
关于您的问题: 1) 确保 pydicom 是否可以读取没有序言的实例。 2)如果是,请尝试弄清楚如何配置它来做到这一点。可能是某些初始配置或某些方法的某些参数或设置某些属性。 3)如果不是,你必须自己插入序言;请参阅上述评论中的链接。
-
@AmitJoshi - 我在答案中添加了一些注释以澄清混淆。错误处理可能会在 pydicom 中更改。
标签: python-3.x dicom pydicom