.. note::
    :class: sphx-glr-download-link-note

    Click :ref:`here <sphx_glr_download_auto_examples_metadata_processing_plot_anonymize.py>` to download the full example code
.. rst-class:: sphx-glr-example-title

.. _sphx_glr_auto_examples_metadata_processing_plot_anonymize.py:


====================
Anonymize DICOM data
====================

This example is a starting point to anonymize DICOM data.

It shows how to read data and replace tags: person names, patient id,
optionally remove curves and private tags, and write the results in a new file.


.. code-block:: python


    # authors : Guillaume Lemaitre <g.lemaitre58@gmail.com>
    # license : MIT

    from __future__ import print_function

    import tempfile

    import pydicom
    from pydicom.data import get_testdata_files

    print(__doc__)







Anonymize a single file
##############################################################################



.. code-block:: python


    filename = get_testdata_files('MR_small.dcm')[0]
    dataset = pydicom.dcmread(filename)

    data_elements = ['PatientID',
                     'PatientBirthDate']
    for de in data_elements:
        print(dataset.data_element(de))





.. rst-class:: sphx-glr-script-out

 Out:

 .. code-block:: none

    (0010, 0020) Patient ID                          LO: '4MR1'
    (0010, 0030) Patient's Birth Date                DA: ''


We can define a callback function to find all tags corresponding to a person
names inside the dataset. We can also define a callback function to remove
curves tags.



.. code-block:: python



    def person_names_callback(dataset, data_element):
        if data_element.VR == "PN":
            data_element.value = "anonymous"


    def curves_callback(dataset, data_element):
        if data_element.tag.group & 0xFF00 == 0x5000:
            del dataset[data_element.tag]








We can use the different callback function to iterate through the dataset but
also some other tags such that patient ID, etc.



.. code-block:: python


    dataset.PatientID = "id"
    dataset.walk(person_names_callback)
    dataset.walk(curves_callback)







pydicom allows to remove private tags using ``remove_private_tags`` method



.. code-block:: python


    dataset.remove_private_tags()







Data elements of type 3 (optional) can be easily deleted using ``del`` or
``delattr``.



.. code-block:: python


    if 'OtherPatientIDs' in dataset:
        delattr(dataset, 'OtherPatientIDs')

    if 'OtherPatientIDsSequence' in dataset:
        del dataset.OtherPatientIDsSequence







For data elements of type 2, this is possible to blank it by assigning a
blank string.



.. code-block:: python


    tag = 'PatientBirthDate'
    if tag in dataset:
        dataset.data_element(tag).value = '19000101'







Finally, this is possible to store the image



.. code-block:: python


    data_elements = ['PatientID',
                     'PatientBirthDate']
    for de in data_elements:
        print(dataset.data_element(de))

    output_filename = tempfile.NamedTemporaryFile().name
    dataset.save_as(output_filename)




.. rst-class:: sphx-glr-script-out

 Out:

 .. code-block:: none

    (0010, 0020) Patient ID                          LO: 'id'
    (0010, 0030) Patient's Birth Date                DA: '19000101'


**Total running time of the script:** ( 0 minutes  0.008 seconds)


.. _sphx_glr_download_auto_examples_metadata_processing_plot_anonymize.py:


.. only :: html

 .. container:: sphx-glr-footer
    :class: sphx-glr-footer-example



  .. container:: sphx-glr-download

     :download:`Download Python source code: plot_anonymize.py <plot_anonymize.py>`



  .. container:: sphx-glr-download

     :download:`Download Jupyter notebook: plot_anonymize.ipynb <plot_anonymize.ipynb>`


.. only:: html

 .. rst-class:: sphx-glr-signature

    `Gallery generated by Sphinx-Gallery <https://sphinx-gallery.readthedocs.io>`_
