使用Python清除DICOM中患者信息

使用Python清除DICOM中患者信息,以下代码由ChatGPT生成,测试通过,需先安装 pydicom 库。
import os
import pydicom
from pydicom.filereader import InvalidDicomError

def clear_patient_info(dicom_file):
    try:
        # 读取DICOM文件
        ds = pydicom.dcmread(dicom_file)
        # 清除患者信息
        if 'PatientName' in ds:
            print(ds.PatientName)
            ds.PatientName = 'Anonymous'
            print(ds.PatientName)
        if 'PatientID' in ds:
            ds.PatientID = 'N0000N'
        if 'PatientBirthDate' in ds:
            print(ds.PatientBirthDate)
            ds.PatientBirthDate = '19700101'
        #if 'PatientSex' in ds:
        #   ds.PatientSex = ''
        # 添加或删除其他需要清除的患者信息标签
        if 'StudyID' in ds:
            ds.StudyID = 'N0000N'
        if 'StudyDate' in ds:
            ds.StudyDate = '19700101'
        if 'StudyTime' in ds:
            ds.StudyTime = '19700101'
        if 'AccessionNumber' in ds:
            ds.AccessionNumber = 'N0000N'
        if 'StudyInstanceUID' in ds:
            ds.StudyInstanceUID = 'N0000N'
        if 'SeriesInstanceUID' in ds:
            ds.SeriesInstanceUID = 'N0000N'
        # 保存修改后的DICOM文件
        ds.save_as(dicom_file)
    except InvalidDicomError:
        print(f"Invalid DICOM file: {dicom_file}")

def clear_patient_info_in_directory(directory):
    for root, dirs, files in os.walk(directory):
        for file in files:
            #if file.lower().endswith('.dcm'):
            dicom_file_path = os.path.join(root, file)
            clear_patient_info(dicom_file_path)
            print(f"Cleared patient info for: {dicom_file_path}")

# 指定DICOM文件所在的目录
directory_path = "./dicom"
clear_patient_info_in_directory(directory_path)

 

posted @ 2024-02-29 15:11  祁连山  阅读(67)  评论(0编辑  收藏  举报