python">"""
dicom文件脱敏脚本(python ), 设置好脱敏字段后, 会将目标目录下所有的.dcm文件脱敏, 并原路径保存,
若需要保存到指定目录, 需要自己修改保存逻辑
"""
import pydicom
from pathlib import Path
from multiprocessing import Pool
from tqdm import tqdm
def desensitization ( file_path) :
dicom_file = pydicom. dcmread( file_path, force= True )
all_names = dicom_file. dir ( )
filter_names = [ 'InstitutionName' , 'InstitutionAddress' , 'ReferringPhysicianName' , 'StudyDescription' ,
'SeriesDescription' , 'InstitutionalDepartmentName' , 'PatientName' , 'PatientBirthDate' ,
'OperatorsName' ]
for filter_name in filter_names:
if filter_name not in all_names:
print ( f"\033[31mfile { file_path} \n { filter_name} is not in { all_names} , please check the input\033[0m" )
continue
setattr ( dicom_file, filter_name, '' )
dicom_file. save_as( str ( file_path) )
if __name__ == '__main__' :
file_paths = list ( Path( "待脱敏的文件根目录" ) . glob( '**/*.dcm' ) )
with Pool( processes= 4 ) as pool:
result = list ( tqdm( pool. imap( desensitization, file_paths) , total= len ( file_paths) ) )