【问题标题】:Error while converting dicom tags in excel using python使用python在excel中转​​换dicom标签时出错
【发布时间】:2018-01-12 20:14:13
【问题描述】:

我正在尝试将 .dcm 文件中的 dicom 标签转换并列出为 excel(使用 python),但某些标签在转换时显示错误(PatientName、PixelSpacing 等)。

一些标签在 excel 文件中显示为“无”,尽管它们包含/显示 Dicom 格式的数据(SliceThickness、SpacingBetweenSlices 等)。我该如何解决这个问题?

此外,我希望输出文件位于不同的文件夹中,而不使用 os.chdir() 命令。我尝试了几种方法,但无法弄清楚。

任何建议将不胜感激。

这是我的代码:

import xlsxwriter 
import sys 
import dicom 
import os.path

root = input("Enter Directory Name: ") path = os.path.join(root, "targetdirectory") i=1

for path, subdirs, files in os.walk(root):
    for name in files:

        os.rename(os.path.join(path, name), os.path.join(path,'MR000'+ str(i)+'.dcm'))
        i=i+1
        print (os.path.join(path, name))

         dcm_files = [] for root, dirs, files in os.walk(path):
    for names in files:
        if names.endswith(".dcm"):
            dcm_files.append(os.path.join(root, names))

for dcm_file in dcm_files:
    ds = dicom.read_file(dcm_file)
    workbook = xlsxwriter.Workbook(os.path.basename(dcm_file) + '.xlsx')
    worksheet = workbook.add_worksheet()
    a = os.getcwd ()
    print (a)


    data = (
            #["PatientName", ds.get("PatientName", "None")],
            ["PatientID", ds.get("PatientID", "None")],
            ["PatientBirthDate", ds.get("PatientBirthDate", "None")],
            ["PatientSex", ds.get("PatientSex", "None")],
            ["RepetitionTime", ds.get("RepetitionTime", "None")],
            ["EchoTime", ds.get("EchoTime", "None")],
            ["Modality", ds.get("Modality", "None")],
            ["Manufacturer", ds.get("Manufacturer", "None")],
            ["InstitutionName", ds.get("Institution Name", "None")],
            #["ReferringPhysicianName", ds.get("ReferringPhysicianName", "None")],
            ["StudyDescription", ds.get("StudyDescription", "None")],
            ["PatientAge", ds.get("PatientAge", "None")],
            ["SliceThickness", ds.get("SliceThickness(mm)", "None")],
            ["SpacingBetweenSlices", ds.get("SpacingBetweenSlices", "None")],
            ["SeriesNumber", ds.get("SeriesNumber", "None")],
            #["PixelSpacing", ds.get("PixelSpacing", "None")],
            ["RequestedProcedureID", ds.get("RequestedProcedureID", "None")],
            ["Rows", ds.get("Rows", "None")],
            ["Columns", ds.get("Columns", "None")]
            )

    row = 0
    col = 0

    for name, value in (data):
        worksheet.write(row, col,     name)
        worksheet.write(row + 1, col, value)
        col += 1

    workbook.close()

来自dicom标签的一些数据:

Columns: 256
InstitutionName: UIC MR2
PatientAge: 046Y
PatientBirthDate: 19630503
PatientID: 080524186
PatientName: PEREZ^GUILLERMINA
PatientPosition: HFS
PatientSex: F
PixelSpacing: ['0.9375', '0.9375']
Rows: 256
SliceThickness: 5
SpacingBetweenSlices: 10
StudyID: 586323788
StudyTime: 204546

谢谢。

【问题讨论】:

    标签: python excel python-3.x xlsx dicom


    【解决方案1】:

    我可以在代码中看到一些错误...例如“SliceThickness(mm)”不是正确的 dicom 关键字,并且“机构名称”有一个空格。我认为最好不要重复关键字。下面的代码将关键字列表设置在循环之外,并使用相同的名称来查找值。它会检查某些无法写入的类型(列表和 PersonName3)并转换它们。列表简单地变成一个字符串,元素之间有逗号;您可以尝试在不同的单元格或其他解决方案中写入。

    import xlsxwriter 
    import sys 
    import dicom 
    import os.path
    from dicom.valuerep import PersonName3
    
    keywords = ("PatientName",
                "PatientID",
                "PatientBirthDate",
                "PatientSex",
                "RepetitionTime",
                "EchoTime",
                "Modality",
                "Manufacturer",
                "InstitutionName",
                "ReferringPhysicianName",
                "StudyDescription",
                "PatientAge",
                "SliceThickness",
                "SpacingBetweenSlices",
                "SeriesNumber",
                "PixelSpacing",
                "RequestedProcedureID",
                "Rows",
                "Columns",
               )
    
    # ...
    # XXX fill in dcm_files list
    dcm_files = [r"C:\temp\test.dcm"]   
    
    for dcm_file in dcm_files:
        ds = dicom.read_file(dcm_file)
        workbook = xlsxwriter.Workbook(os.path.basename(dcm_file) + '.xlsx')
        worksheet = workbook.add_worksheet()
    
        row = 0
        col = 0
    
        for keyword in keywords:
            value = ds.get(keyword, "None")
            if isinstance(value, list):
                value = ", ".join([str(x) for x in value])
            elif isinstance(value, PersonName3):
                value = str(value)
            worksheet.write(row, col, keyword)
            worksheet.write(row + 1, col, value)
            col += 1
    
        workbook.close()
    

    我更改了 dcm_files 部分只是为了进行更简单的测试;您可以放回完整列表的代码。

    我不知道为什么 SpacingBetweenSlices 在您的代码中不起作用,但从我的测试文件中可以看出它很好。

    至于输出到不同的文件夹,您应该可以使用 os.path.join 与您想要的位置和您创建的 xlsx 文件名。

    【讨论】:

    • 非常感谢您提供的代码。我在编译时面临的另一个问题是,如果我在每次重命名原始文件后多次运行该程序,我的原始 .dcm 文件就会丢失。你知道为什么会这样吗?
    • 我不确定您的意思 - 但如果您重命名文件,那么原始名称将不再存在。也许您的意思是复制?在任何情况下都不需要重命名 - pydicom 读取保持原始文件不变。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-08
    • 1970-01-01
    • 2018-11-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多