使用python的pydicom模块对DICOMDIR文件进行操作,将混合存储的DICOM图像转化为分层存储

混合存储的DICOM图像,包含多个病人,多个研究次数(检查次数),多个和多个序列,多个图像。
但多个病人和多个序列的图像混在一起存放,较难分辨哪些图片属于同一个病人。
可以根据配套的DICOMDIR文件,使用python的pydicom模块对DICOMDIR文件进行操作,将上述DICOM图像转化为分层存储。

运行前:
在这里插入图片描述
运行后:
在这里插入图片描述

运行代码前务必备份原文件,偶尔会因DICOM图像所在的目录名错误,导致程序中止

唯一需要修改的地方是第7行path = ‘ ’中填写DICOMDIR文件的地址,如C:\DICOMDIR

import os
from os.path import dirname, join
import shutil
import pydicom
from pydicom.filereader import read_dicomdir
import re

path = ' '
dicom_dir = read_dicomdir(path)
base_dir = dirname(path)

for patient_record in dicom_dir.patient_records:  # got through each patient
    path1 = os.path.join(base_dir, patient_record.PatientName.family_name)#拼接目录
    try: os.mkdir(path1) # 创建目录
    except FileExistsError: pass

    studies = patient_record.children  # got through each study
    for study in studies:
        path2 = os.path.join(path1, study.StudyID)
        try: os.mkdir(path2)  # 创建目录
        except FileExistsError: pass

        all_series = study.children  # go through each serie
        for series in all_series:
            series.SeriesDescription = re.sub(r'\W', "", series.SeriesDescription)#偶尔存在非法符号,正则匹配后去掉
            path3 = os.path.join(path2, series.SeriesDescription)
            try: os.mkdir(path3)  # 创建目录
            except FileExistsError: pass

            image_records = series.children  # go through each image
            image_filenames = [join(base_dir, *image_rec.ReferencedFileID) for image_rec in image_records]
            for image_filename in image_filenames:
                try: shutil.move(image_filename, path3)#移动文件
                except shutil.Error: pass
                except FileNotFoundError: pass               
  • 2
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

dr_yingli

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

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

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

打赏作者

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

抵扣说明:

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

余额充值