
.. DO NOT EDIT.
.. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY.
.. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE:
.. "auto_examples/metadata_processing/plot_add_dict_entries.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_add_dict_entries.py>`
        to download the full example code.

.. rst-class:: sphx-glr-example-title

.. _sphx_glr_auto_examples_metadata_processing_plot_add_dict_entries.py:


=========================================
Add items to the private DICOM dictionary
=========================================

This examples illustrates how to add private dictionary items to the DICOM
dictionary dynamically. This allows you to read private tags not present in
pydicom's private dictionary when loading an existing dataset.

.. GENERATED FROM PYTHON SOURCE LINES 10-77




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

 .. code-block:: none


    Output for unknown private tags:
    (1001,0010) Private Creator                     LO: 'ACME 3.1'
    (1001,1001) Private tag data                    UN: b'*\x00\x00\x00'
    (1001,1002) Private tag data                    UN: b'Hello World '
    (1001,1003) Private tag data                    UN: b'1.2.3.4.5\x00'

    Output with registered private tags:
    (1001,0010) Private Creator                     LO: 'ACME 3.1'
    (1001,1001) [Test One]                          UL: 42
    (1001,1002) [Test Two]                          SH: 'Hello World'
    (1001,1003) [Test Three]                        UI: 1.2.3.4.5






|

.. code-block:: Python


    # authors : Darcy Mason and pydicom contributors
    # license : MIT

    import io

    from pydicom import dcmread
    from pydicom.datadict import add_private_dict_entries
    from pydicom.dataset import Dataset
    from pydicom.valuerep import VR

    print(__doc__)

    # create a dataset with some private tags for demonstration
    # we create the dataset with Implicit VR Little Endian transfer syntax,
    # so that the VR of the private tags will not be saved

    ds = Dataset()
    ds.is_implicit_VR = True
    ds.is_little_endian = True

    # add private tags by creating a new private block and add elements to it
    block = ds.private_block(0x1001, "ACME 3.1", create=True)
    block.add_new(0x01, VR.UL, 42)
    block.add_new(0x02, VR.SH, "Hello World")
    block.add_new(0x03, VR.UI, "1.2.3.4.5")

    # write the dataset into a memory file and read it back
    # this simulates reading from a normal DICOM file
    fp = io.BytesIO()
    ds.save_as(fp)
    ds = dcmread(fp, force=True)

    print("Output for unknown private tags:")
    print(ds)

    # Creates output:
    # (1001,0010) Private Creator                     LO: 'ACME 3.1'
    # (1001,1001) Private tag data                    UN: b'*\x00\x00\x00'
    # (1001,1002) Private tag data                    UN: b'Hello World '
    # (1001,1003) Private tag data                    UN: b'1.2.3.4.5\x00'


    # Add the private tags to the private tag dictionary

    # Define items as (VR, VM, description, is_retired flag)
    # Leave is_retired flag blank.
    new_dict_items = {
        0x10011001: ("UL", "1", "Test One", ""),
        0x10011002: ("SH", "1", "Test Two", ""),
        0x10011003: ("UI", "1", "Test Three", ""),
    }

    # add the entries to the private dictionary, using the correct private creator string
    add_private_dict_entries(private_creator="ACME 3.1", new_entries_dict=new_dict_items)

    # re-read the dataset for the new dictionary entries to be applied
    ds = dcmread(fp, force=True)

    print("\nOutput with registered private tags:")
    print(ds)

    # Creates output:
    # (1001,0010) Private Creator                     LO: 'ACME 3.1'
    # (1001,1001) [Test One]                          UL: 42
    # (1001,1002) [Test Two]                          SH: 'Hello World'
    # (1001,1003) [Test Three]                        UI: 1.2.3.4.5


.. rst-class:: sphx-glr-timing

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


.. _sphx_glr_download_auto_examples_metadata_processing_plot_add_dict_entries.py:

.. only:: html

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

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

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

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

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

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

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


.. only:: html

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

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