读取dicom 字段信息

在这里插入代码片
```import os
import pydicom
import pandas as pd
import xlwt


DataRootPath = r"C:\Users\Desktop\DicomTest"
# SaveCsvPath = "C:\\PersonalDocument1\\TemporaryMaterial\\test.csv"
df = pd.DataFrame()
NewPath = ""


def ReadDcmTagVal(dcm, attr1, attr2):
    try:
        val = dcm[attr1, attr2].value
        return val
    except:
        val = ""
        return val


def AddToDf(DcmFile, NewPath):
    PatientsName = ReadDcmTagVal(DcmFile, 0x0010, 0x0010)
    PatientID = ReadDcmTagVal(DcmFile, 0x0010, 0x0020)
    StudyID = ReadDcmTagVal(DcmFile, 0x0020, 0x0010)

    # PatientSex = ReadDcmTagVal(DcmFile, 0x0010, 0x0040)
    # PatientAge = ReadDcmTagVal(DcmFile, 0x0010, 0x1010)
    # BodyPartExamined = ReadDcmTagVal(DcmFile, 0x0018, 0x0015)
    # ScanOptions = ReadDcmTagVal(DcmFile, 0x0018, 0x0022)
    # ProtocolName = ReadDcmTagVal(DcmFile, 0x0018, 0x1030)
    # PatientPosition = ReadDcmTagVal(DcmFile, 0x0018, 0x5100)
    # # ImplementationVersionName = DcmFile[0x0002, 0x0013].value
    # ImageType = ReadDcmTagVal(DcmFile, 0x0008, 0x0008)
    # Modality = ReadDcmTagVal(DcmFile, 0x0008, 0x0060)
    # InstitutionName = ReadDcmTagVal(DcmFile, 0x0008, 0x0080)
    # ManufacturerModelName = ReadDcmTagVal(DcmFile, 0x0008, 0x1090)
    # SliceThickness = ReadDcmTagVal(DcmFile, 0x0018, 0x0050)
    # Contrast = ReadDcmTagVal(DcmFile, 0x0018, 0x0010)
    # ScanLength = ReadDcmTagVal(DcmFile, 0x01f1, 0x1008)


    # s = {"Path": NewPath, "PatientSex": PatientSex, "PatientAge": PatientAge, "BodyPartExamined": BodyPartExamined,
    #      "ScanOptions": ScanOptions, "ProtocolName": ProtocolName, "PatientPosition": PatientPosition,
    #      "ImageType": ImageType, "Modality": Modality,
    #      "InstitutionName": InstitutionName, "ManufacturerModelName": ManufacturerModelName,
    #      "SliceThickness": SliceThickness, "Contrast": Contrast, "ScanLength": ScanLength}

    s = {"Path": NewPath, "PatientsName": PatientsName, "PatientID": PatientID, "StudyID": StudyID}
    global df
    df = df.append(s, ignore_index=True)



def SearchDicomFile(DataPath):
    global NewPath
    try:
        if (os.path.isdir(DataPath)):
            Files = os.listdir(DataPath)
            for i in range(0, len(Files)):
                NewPath = DataPath + "\\" + Files[i]
                if (os.path.isdir(NewPath)):
                    SearchDicomFile(NewPath)
                elif (os.path.isfile(NewPath)):
                    try:
                        DcmFile = pydicom.dcmread(NewPath)
                        print(DcmFile)
                        C = str(DcmFile)
                        txt = open(r"C:\Users\320133514\Desktop\DicomTest\Incisive1.1_Data_Informationdicominformation.txt", "w").write(C)
                        AddToDf(DcmFile, NewPath)
                        print(NewPath)
                        break;
                    except:
                            pass
                    continue


        else:
            print(NewPath + ":ERROR!")
    except:
        print(NewPath + ":ERROR!!!")


if __name__ == '__main__':
    SearchDicomFile(DataRootPath)
    # df.to_csv("C:\\Users\\Desktop\\DicomTest\\425test.xlsx", encoding="utf_8_sig")
    df.to_excel(r"C:\User\Desktop\DicomTest\Incisive1.1_Data_Information.xlsx", encoding="utf_8_sig")
    # df.to_json("C:\\Users\\Desktop\\DicomTest\\425test.json")


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Fly*Boy

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值