Read dicom file to obtain internal information to save, save a person time

 

 

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()

 

Published 74 original articles · won praise 64 · views 130 000 +

Guess you like

Origin blog.csdn.net/wsLJQian/article/details/105114506