Skip to main content
Release file added late

1 file was added to this release more than 14 days after its initial publication. Inspect the release files before installing.

DicomAnonymizer

Python package to anonymize DICOM files. The anonymization answer to the standard . More information about dicom fields for anonymization can be found here.

The default behaviour of this package is to anonymize DICOM fields referenced in dicomfields.

Dicom fields are separated into different groups. Each groups will be anonymized in a different way.

Group Action Action definition
D_TAGS replace Replace with a non-zero length value that may be a dummy value and consistent with the VR**
Z_TAGS empty Replace with a zero length value, or a non-zero length value that may be a dummy value and consistent with the VR**
X_TAGS delete Completely remove the tag
U_TAGS replace_UID Replace all UID's number with a random one in order to keep consistent. Same UID will have the same replaced value
Z_D_TAGS empty_or_replace Replace with a non-zero length value that may be a dummy value and consistent with the VR**
X_Z_TAGS delete_or_empty Replace with a zero length value, or a non-zero length value that may be a dummy value and consistent with the VR**
X_D_TAGS delete_or_replace Replace with a non-zero length value that may be a dummy value and consistent with the VR**
X_Z_D_TAGS delete_or_empty_or_replace Replace with a non-zero length value that may be a dummy value and consistent with the VR**
X_Z_U_STAR_TAGS delete_or_empty_or_replace_UID If it's a UID, then all numbers are randomly replaced. Else, replace with a zero length value, or a non-zero length value that may be a dummy value and consistent with the VR**
ALL_TAGS Contains all previous defined tags

How to build it ?

The sources files can be packaged by using: python ./setup.py bdist_wheel

This command will generate a wheel package in dist folder which can be then installed as a python package using pip install ./dist/dicom_anonymizer-1.0.7-py2.py3-none-any.whl

Installing this package will also install an executable named dicom-anonymizer. In order to use it, please refer to the next section.

How to use it ?

This package allows to anonymize a selection of DICOM field (defined or overrided). The way on how the DICOM fields are anonymized can also be overrided.

  • [required] InputPath = Full path to a single DICOM image or to a folder which contains dicom files
  • [required] OutputPath = Full path to the anonymized DICOM image or to a folder. This folder has to exist.
  • [optional] ActionName = Defined an action name that will be applied to the DICOM tag.
  • [optional] Dictionary = Path to a JSON file which defines actions that will be applied on specific dicom tags (see below)

Default behaviour

You can use the default anonymization behaviour describe above.

dicom-anonymizer Input Output

Private tags

Default behavior of the dicom anonymizer is to delete private tags. But you can bypass it:

  • Solution 1: Use regexp to define which private tag you want to keep/update (cf custom rules)
  • Solution 2: Use dicom-anonymizer.exe option to keep all private tags : --keepPrivateTags

Custom rules

You can manually add new rules in order to have different behaviors with certain tags. This will allow you to override default rules:

Executable:

dicom-anonymizer InputFilePath OutputFilePath -t '(0x0001, 0x0001)' ActionName -t '(0x0001, 0x0005)' ActionName2

This will apply the ActionName to the tag '(0x0001, 0x0001)' and ActionName2 to '(0x0001, 0x0005)'

Note: ActionName has to be defined in actions list

Example 1: The default behavior of the patient's ID is to be replaced by an empty or null value. If you want to keep this value, then you'll have to run :

python anonymizer.py InputFilePath OutputFilePath -t '(0x0010, 0x0020)' keep

This command will override the default behavior executed on this tag and the patient's ID will be kept.

Example 2: We just want to change the study date from 20080701 to 20080000, then we'll use the regexp

python anonymizer.py InputFilePath OutputFilePath -t '(0x0008, 0x0020)' 'regexp' '0701$' '0000'

Custom rules with dictionary file

Instead of having a big command line with several new actions, you can create your own dictionary by creating a json file dictionary.json :

{
    "(0x0002, 0x0002)": "ActionName",
    "(0x0003, 0x0003)": "ActionName",
    "(0x0004, 0x0004)": "ActionName",
    "(0x0005, 0x0005)": "ActionName"
}

Same as before, the ActionName has to be defined in the actions list.

dicom-anonymizer InputFilePath OutputFilePath --dictionary dictionary.json

If you want to use the regexp action in a dictionary:

{
    "(0x0002, 0x0002)": "ActionName",
    "(0x0008, 0x0020)": {
        "action": "regexp",
        "find": "0701$",
        "replace": "0000"
    }
}

Custom/overrides actions

Here is a small example which keeps all metadata but updates the series description by adding a suffix passed as a parameter.

import argparse
from dicomanonymizer import *

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')
    parser.add_argument('--suffix', action='store', help='Suffix that will be added at the end of series description')
    args = parser.parse_args()

    input_dicom_path = args.input
    output_dicom_path = args.output

    extraAnonymizationRules = {}

    def setupSeriesDescription(dataset, tag):
        element = dataset.get(tag)
        if element is not None:
            element.value = element.value + '-' + args.suffix

    # ALL_TAGS variable is defined on file dicomfields.py
    # the 'keep' method is already defined into the dicom-anonymizer
    # It will overrides the default behaviour
    for i in allTags:
        extraAnonymizationRules[i] = keep

    if args.suffix:
        extraAnonymizationRules[(0x0008, 0x103E)] = setupSeriesDescription

    # Launch the anonymization
    anonymize(input_dicom_path, output_dicom_path, extraAnonymizationRules)

if __name__ == "__main__":
    main()

In your own file, you'll have to define:

  • Your custom functions. Be careful, your functions always have in inputs a dataset and a tag
  • A dictionary which map your functions to a tag

Anonymize dicom tags without dicom file

If for some reason, you need to anonymize dicom fields without initial dicom file (extracted from a database for example). Here is how you can do it:

from dicomanonymizer import *

def main():

  # Create a list of tags object that should contains id, type and value
  fields = [
    { # Replaced by Anonymized
      'id': (0x0040, 0xA123),
      'type': 'LO',
      'value': 'Annie de la Fontaine',
    },
    { # Replaced with empty value
      'id': (0x0008, 0x0050),
      'type': 'TM',
      'value': 'bar',
    },
    { # Deleted
      'id': (0x0018, 0x4000),
      'type': 'VR',
      'value': 'foo',
    }
  ]

  # Create a readable dataset for pydicom
  data = pydicom.Dataset()

  # Add each field into the dataset
  for field in fields:
    data.add_new(field['id'], field['type'], field['value'])

  anonymize_dataset(data)

if __name__ == "__main__":
    main()

For more information about the pydicom's Dataset, please refer here. You can also add a dictionnary as previously :

    dictionary = {}

    def newMethod(dataset, tag):
        element = dataset.get(tag)
        if element is not None:
            element.value = element.value + '- generated with new method'

    dictionary[(0x0008, 0x103E)] = newMethod
    anonymize_dataset(data, dictionary)

Actions list

Action Action definition
empty Replace with a zero length value, or a non-zero length value that may be a dummy value and consistent with the VR**
delete Completely remove the tag
keep Do nothing on the tag
clean Don't use it for now. This is not implemented
replace_UID Replace all UID's number with a random one in order to keep consistent. Same UID will have the same replaced value
empty_or_replace Replace with a non-zero length value that may be a dummy value and consistent with the VR**
delete_or_empty Replace with a zero length value, or a non-zero length value that may be a dummy value and consistent with the VR**
delete_or_replace Replace with a non-zero length value that may be a dummy value and consistent with the VR**
deleteOrEmplyOrReplace Replace with a non-zero length value that may be a dummy value and consistent with the VR**
delete_or_empty_or_replace_UID If it's a UID, then all numbers are randomly replaced. Else, replace with a zero length value, or a non-zero length value that may be a dummy value and consistent with the VR**
regexp These action is not a common action. It allows to use regexp to modify values

** VR: Value Representation

Work originally done by Edern Haumont

Metadata

Release files for dicom-anonymizer 1.0.9

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

File added late

1 file was uploaded more than 14 days after the first file in this release.

While project maintainers occasionally add legitimate files to an existing release, late additions can also indicate a security compromise.

We recommend inspecting the release files before installing.

Source distribution (sdist)

Source distribution for dicom-anonymizer 1.0.9
File Size Uploaded Provenance Actions
dicom_anonymizer-1.0.9.tar.gz 15.9 kB File added late: Without provenance Details for dicom_anonymizer-1.0.9.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for dicom-anonymizer 1.0.9
File Interpreter ABI Platform Actions
dicom_anonymizer-1.0.9-py2.py3-none-any.whl Python 2, Python 3 none any Details for dicom_anonymizer-1.0.9-py2.py3-none-any.whl

Total release size: 31.3 kB

Release files / dicom_anonymizer-1.0.9.tar.gz

File added late

This file was uploaded more than 14 days after the first file in this release.

While project maintainers occasionally add legitimate files to an existing release, late additions can also indicate a security compromise.

We recommend inspecting the release file before installing.

Download URL dicom_anonymizer-1.0.9.tar.gz
Size 15.9 kB
Tags Source
SHA-256 checksum
How to use checksums
15d6cc98e3899288dfb9b6081a18d991e25a0e28d4ee0a1407de56ff10154ca9
BLAKE2b-256 checksum
How to use checksums
a2d36adcfcbf5847a7fc5c3609535ec58c4e6fc16c9b8778da3686b4819001ee
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.9.13

Release files / dicom_anonymizer-1.0.9-py2.py3-none-any.whl

Download URL dicom_anonymizer-1.0.9-py2.py3-none-any.whl
Size 15.3 kB
Tags Python 2 Python 3
SHA-256 checksum
How to use checksums
551e4ac60735509d97aa6844286b31a67376004c23c2d38969aeca85b9a39d01
BLAKE2b-256 checksum
How to use checksums
fff2300691c34d02b573d6dd38eaf510103b79cffdb0144b37a935375710ee27
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.4.0 importlib_metadata/3.7.3 packaging/20.9 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.59.0 CPython/3.8.7

Security for dicom-anonymizer 1.0.9

Upload timing

Release file added late

1 file was uploaded more than 14 days after the first file in this release.

While project maintainers occasionally add legitimate files to an existing release, late additions can also indicate a security compromise.


Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page