
.. DO NOT EDIT.
.. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY.
.. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE:
.. "auto_examples/metadata_processing/plot_anonymize.py"
.. LINE NUMBERS ARE GIVEN BELOW.

.. only:: html

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

        :ref:`Go to the end <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.

.. GENERATED FROM PYTHON SOURCE LINES 14-16

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

.. GENERATED FROM PYTHON SOURCE LINES 16-30

.. code-block:: Python


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

    import tempfile

    from pydicom import examples

    ds = examples.mr

    for keyword in ["PatientID", "PatientBirthDate"]:
        print(ds.data_element(keyword))





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

 .. code-block:: none

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




.. GENERATED FROM PYTHON SOURCE LINES 31-34

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.

.. GENERATED FROM PYTHON SOURCE LINES 34-46

.. code-block:: Python



    def person_names_callback(ds, elem):
        if elem.VR == "PN":
            elem.value = "anonymous"


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









.. GENERATED FROM PYTHON SOURCE LINES 47-49

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

.. GENERATED FROM PYTHON SOURCE LINES 49-54

.. code-block:: Python


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








.. GENERATED FROM PYTHON SOURCE LINES 55-56

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

.. GENERATED FROM PYTHON SOURCE LINES 56-59

.. code-block:: Python


    ds.remove_private_tags()








.. GENERATED FROM PYTHON SOURCE LINES 60-62

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

.. GENERATED FROM PYTHON SOURCE LINES 62-69

.. code-block:: Python


    if "OtherPatientIDs" in ds:
        delattr(ds, "OtherPatientIDs")

    if "OtherPatientIDsSequence" in ds:
        del ds.OtherPatientIDsSequence








.. GENERATED FROM PYTHON SOURCE LINES 70-72

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

.. GENERATED FROM PYTHON SOURCE LINES 72-77

.. code-block:: Python


    tag = "PatientBirthDate"
    if tag in ds:
        ds.data_element(tag).value = "19000101"








.. GENERATED FROM PYTHON SOURCE LINES 78-79

Finally, this is possible to store the image

.. GENERATED FROM PYTHON SOURCE LINES 79-85

.. code-block:: Python


    for keyword in ["PatientID", "PatientBirthDate"]:
        print(ds.data_element(keyword))

    path = tempfile.NamedTemporaryFile().name
    ds.save_as(path)




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

 .. code-block:: none

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





.. _sphx_glr_download_auto_examples_metadata_processing_plot_anonymize.py:

.. only:: html

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

    .. container:: sphx-glr-download sphx-glr-download-jupyter

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

    .. container:: sphx-glr-download sphx-glr-download-python

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

    .. container:: sphx-glr-download sphx-glr-download-zip

      :download:`Download zipped: plot_anonymize.zip <plot_anonymize.zip>`


.. only:: html

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

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