Skip to content

Instantly share code, notes, and snippets.

@rlizzo
Created December 6, 2017 08:43
Show Gist options
  • Save rlizzo/bd02d7b2a84994d292ac1c12b8b7be27 to your computer and use it in GitHub Desktop.
Save rlizzo/bd02d7b2a84994d292ac1c12b8b7be27 to your computer and use it in GitHub Desktop.
Anonymize DICOM data
'''
DicomNoMeta.py
For University Hospital in Krakow DICOMs
Jan Witowski / Garage of Complexity
Usage: DicomNoMeta.py <path>
Anonymizes all personal data
'''
import dicom
import glob
import os, sys
from os.path import join, expanduser
if len(sys.argv)==1:
print("Define path")
sys.exit(0)
dicomPath = sys.argv[1]
os.chdir(dicomPath)
for file in glob.glob("*.dcm"):
print("### Now reading file:", file)
tempPath = join(dicomPath, file)
df = dicom.read_file(tempPath)
df[0x0038, 0x0300].value = "UNKNOWN" # current patient location
df[0x0033, 0x1013].value = "UNKNOWN".encode() # patient's name - encode unicode string to byte string (b"")
df[0x0032, 0x1033].value = "UNKNOWN" # requesting service
df[0x0032, 0x1032].value = "UNKNOWN" # requesting physician
df[0x0020, 0x0010].value = "UNKNOWN" # study ID (?)
df[0x0010, 0x0010].value = "UNKNOWN" # patient's name
df[0x0010, 0x0030].value = "UNKNOWN" # patient's birth date
df[0x0010, 0x0040].value = "UNKNOWN" # patient's sex
df[0x0008, 0x0080].value = "UNKNOWN" # institution name
tempPathEdit = join(dicomPath, "cleaned", "c_"+file)
df.save_as(tempPathEdit)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment