其他分享
首页 > 其他分享> > 整理dicom目录

整理dicom目录

作者:互联网

# -*- coding=utf8 -*-
__author__ = 'ken'
import os
import pydicom
import shutil
# from workserver.utils.uii_print import uii_print


class ArchiveDicomFile:

    @classmethod
    def judge_is_dicom_path(cls, dicom_file_path):
        if os.path.isfile(dicom_file_path):
            return True
        else:
            return False

    @classmethod
    def get_dicom_file_path(cls, file_path, dicom_path_list):
        if cls.judge_is_dicom_path(file_path):
            dicom_path_list.append(file_path)
        else:
            for file_name in os.listdir(file_path):
                new_file_path = os.path.join(file_path, file_name)
                cls.get_dicom_file_path(new_file_path, dicom_path_list)

    @classmethod
    def read_data_from_dicom(cls, dicom_path):
        dcm_info = {}
        try:
            dcm = pydicom.read_file(dicom_path)
            dcm_info['study_id'] = dcm.StudyInstanceUID
            dcm_info['series_id'] = dcm.SeriesInstanceUID
        except:
            os.remove(dicom_path)
        return dcm_info

    @classmethod
    def archive_dicom_file(cls, input_path, output_path):
        if not os.path.exists(output_path):
            os.makedirs(output_path)
        dicom_path_list = []
        cls.get_dicom_file_path(input_path, dicom_path_list)
        for dicom_path in dicom_path_list:
            dcm_info = cls.read_data_from_dicom(dicom_path)
            if len(dcm_info) > 0:
                series_path = os.path.join(output_path, dcm_info['study_id'], dcm_info['series_id'])
                if not os.path.exists(series_path):
                    os.makedirs(series_path)
                print('copy ', series_path)
                shutil.copy(dicom_path, series_path)
            else:
                pass


if __name__ == "__main__":
    input_path = r'C:\Users\ruigang.zhang\Desktop\头颈CTA\源数据'
    out_path = r'C:\Users\ruigang.zhang\Desktop\头颈CTA\test'
    ArchiveDicomFile.archive_dicom_file(input_path, out_path)

标签:dcm,dicom,file,整理,path,os,目录,cls
来源: https://blog.csdn.net/zrg3699/article/details/120287765