gpt4 book ai didi

python-3.x - 如何修复 pydicom 的 dcmread() 序言错误?

转载 作者:行者123 更新时间:2023-12-05 08:49:41 25 4
gpt4 key购买 nike

我在给定路径中存在的 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 的贡献者。

错误发生是因为 get_testdata_file 返回 None,然后将其用作文件名。错误消息本身有点误导 - 在这种情况下引发 FileNotFoundValueError 可能会更好(Update:它引发 TypeError 在当前的 pydicom 中)。相反发生的是,首先读取的是前导码(DICOM 图像的标记,由 128 个零字节后跟“DICM”组成),并且在尝试读取它时由于文件指针

这是 documentation for get_testdata_file :

Return an absolute path to the first matching dataset with filename name.

First searches the local pydicom data store, then any locally available external sources, and finally the files available in the pydicom/pydicom-data repository.

所以这是一个方便的功能,主要是为了查找pydicom测试数据文件。
如果你已经有了文件路径,你可以直接使用它:

import pydicom

fn = "/exact/path/hi.dcm"
ds = pydicom.dcmread(fn)

至于序言,如果 DICOM 文件没有序言(旧的 ACR-NEMA 格式),您可以使用 force=True 参数读取此类文件:

ds = pydicom.dcmread(fn, force=True)

虽然我只会在你真的有这样的文件时才使用它,因为它也会尝试将非 DICOM 文件作为 DICOM 来处理,这可能会导致一些意外的异常。如前所述,这种情况下的错误与缺少前导码无关,而是与不正确的文件路径有关。

更新:
pydicom 2.1 版以来,此行为已更改。如果传递 Nonedcmread 现在将引发 TypeError 和一条有意义的消息。

关于python-3.x - 如何修复 pydicom 的 dcmread() 序言错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63463575/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com