KitwareMedical / dicom-anonymizer

Tool to anonymize DICOM files according to the DICOM standard
BSD 3-Clause "New" or "Revised" License
104 stars 47 forks source link

Any easy way anonymize all DT tags? #78

Closed mkzia closed 5 months ago

mkzia commented 6 months ago

Hello,

Is it possible to anonymize all DT tags without having to specify each tag?

Thanks

pchoisel commented 6 months ago

Hi,

There is not easy way with just the CLI.
However, you can start with the anonymize_extra_rules.py example and tweak the anonymizing function to get the desired result

import argparse

from dicomanonymizer.dicomfields import ALL_TAGS
from dicomanonymizer import anonymize

def main():
    parser = argparse.ArgumentParser(add_help=True)
    parser.add_argument(
        "input",
        help="Path to the input dicom file or input directory which contains dicom files",
    )
    parser.add_argument(
        "output",
        help="Path to the output dicom file or output directory which will contains dicom files",
    )
    args = parser.parse_args()

    input_dicom_path = args.input
    output_dicom_path = args.output

    extra_anonymization_rules = {}

    def anonymize_if_DT(dataset, tag):
        element = dataset.get(tag)
        if element is not None and element.VR == "DT":
            # Change value to whatever you need
            element.value = "00000000"  # YYYYMMDD format

    # ALL_TAGS variable is defined on file dicomfields.py
    for i in ALL_TAGS:
        extra_anonymization_rules[i] = anonymize_if_DT

    # Launch the anonymization
    anonymize(
        input_dicom_path,
        output_dicom_path,
        extra_anonymization_rules,
        delete_private_tags=False,
    )

if __name__ == "__main__":
    main()