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

Help: Passing values to function #49

Closed Ede1994 closed 1 year ago

Ede1994 commented 1 year ago

I would like to use the following function:

def setupSeriesDescription(dataset, tag, value):
    r'''
    Modify the series description by adding a suffix
    '''
    element = dataset.get(tag)
    if element is not None:
        element.value = element.value + '-' + value

and then use them as follows:

def anonymize_dicom(src_path, dst_path):
    # List the files' names that we want to extract data from
    dicom_files = glob.glob(os.path.join(src_path, "**" ,'*.dcm'), recursive = True)

    # Iterate over each DICOM file in the folder and read it using dcmread()
    for file_path in dicom_files:
        # dictionary which map your functions to a tag
        extraAnonymizationRules = {}

        if True:
            # series description
            extraAnonymizationRules[(0x0008, 0x103E)] = setupSeriesDescription

        # Launch the anonymization and delete all private tags
        dcm = anonymize(file_path, dst_path, extraAnonymizationRules, deletePrivateTags=True)

How can I now use the variable value to be able to append something to the series description that differs from dataset to dataset?

Ede1994 commented 1 year ago

Next time I'll think about it before and then I'm sure I'll find my mistake :) but maybe it will help someone later. The following works of course:

extraAnonymizationRules[(0x0008, 0x103E)] = lambda dataset, tag, value=value: setupSeriesDescription(dataset, tag, value)
pchoisel commented 1 year ago

Glad your problem got solved !