讀取dicom文件,獲取內部信息進行保存,一個人保存一次

 

 

def rename_temp():
    img_PATH = r"./test"

    flag = "(1)"
    csvfile = open(r'./patientID.csv', 'w', newline='')
    for path, dirs, files in os.walk(img_PATH):
        for filename in files:  # 遍歷所有文件
            num = ((filename.split("_")[1]).split(")")[0]).split("(")[-1]

            file_path = os.path.join(path, filename)
            ds = pydicom.dcmread(file_path, force=True)  # 讀取dcm

            patient_id = str(ds.PatientID)
            patient_sex = str(ds.PatientSex)
            patient_age = str(ds.PatientAge)

            if patient_id != flag:
                print(num)
                print("{}***{}***{}".format(patient_id, patient_sex, patient_age))
                writer = csv.writer(csvfile)
                writer.writerow([num, patient_id,patient_sex,patient_age])
                flag = patient_id
            else:
                pass
    csvfile.close()

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章