Add delete_private_tags option to dicom_anon(). Verified both True and False case on a CT image taken on a Toshiba Aquilion.
@@ -29,7 +29,7 @@ import pydicom
from .._level0.libutils import get_imports
IMPORTS = get_imports(globals())
-def dicom_anon(dcm, tags_to_keep = []):
+def dicom_anon(dcm, delete_private_tags = True, tags_to_keep = []):
r"""A simple tool to anonymise a DICOM file.
Parameters
@@ -39,13 +39,18 @@ def dicom_anon(dcm, tags_to_keep = []):
pydicom.dataset.FileDataset - ordinarily returned by pydicom.dcmread().
`dcm` must represent a valid DICOM file.
+ delete_private_tags : boolean
+ Flags whether to remove all private (non-standard) DICOM tags
+ from the DICOM file. These may also contain identifying information.
+ Defaults to True.
+
tags_to_keep : array_like
A sequence of DICOM tags to exclude from anonymisation. Empty by default.
Returns
-------
dcm_out : pydicom.dataset.FileDataset
- The anonymised copy of the input DICOM file
+ An anonymised copy of the input DICOM file as a pydicom FileDataset
Raises
------
@@ -53,6 +58,7 @@ def dicom_anon(dcm, tags_to_keep = []):
If `dcm` is not an instance of pydicom.dataset.FileDataset
"""
+ # Is this a pydicom FileDataset?
if not isinstance(dcm, pydicom.dataset.FileDataset):
raise TypeError("The input argument is a member of {}. " +
"It must be a pydicom FileDataset.".format(type(dcm)))
@@ -113,6 +119,13 @@ def dicom_anon(dcm, tags_to_keep = []):
"InstanceCreationTime",
"InstanceCreatorUID"]
+ dcm_out = copy.deepcopy(dcm)
+
+ # Remove private tags from DICOM file unless requested not to.
+ if delete_private_tags:
+ dcm_out.remove_private_tags()
+
+ # Exclude tags from anonymisation process that have been requested to remain as is
for tag in tags_to_keep:
try:
tags_to_anonymise.remove(tag)
@@ -120,8 +133,8 @@ def dicom_anon(dcm, tags_to_keep = []):
# Value not in list. TODO: Warn?
pass
- dcm_out = copy.deepcopy(dcm)
-
+ # Overwrite tags in anonymisation list with an empty string.
+ # TODO: Provide alternative overwrite values?
for tag in tags_to_anonymise:
if hasattr(dcm_out, tag):
setattr(dcm_out, tag, "")
