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

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

.. _sphx_glr_auto_examples_input_output_plot_write_dicom.py:


================
Write DICOM data
================

This example shows how to write a DICOM file from scratch using pydicom. This
example does not produce a DICOM standards compliant file as written, you will
have to change UIDs to valid values and add all required DICOM data elements.

.. GENERATED FROM PYTHON SOURCE LINES 11-54




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

 .. code-block:: none

    Setting dataset values...
    Setting file meta information...
    Writing dataset to tempfile
    Load dataset from: tempfile ...
    Dataset.file_meta -------------------------------
    (0002,0000) File Meta Information Group Length  UL: 128
    (0002,0001) File Meta Information Version       OB: b'\x00\x01'
    (0002,0002) Media Storage SOP Class UID         UI: CT Image Storage
    (0002,0003) Media Storage SOP Instance UID      UI: 1.2.3
    (0002,0010) Transfer Syntax UID                 UI: Explicit VR Little Endian
    (0002,0012) Implementation Class UID            UI: 1.2.3.4
    (0002,0013) Implementation Version Name         SH: 'PYDICOM 0.0.0'
    -------------------------------------------------
    (0008,0023) Content Date                        DA: '20000101'
    (0010,0010) Patient's Name                      PN: 'Test^Firstname'
    (0010,0020) Patient ID                          LO: '123456'
    Deleting file: tempfile ...






|

.. code-block:: Python


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

    import datetime
    from pathlib import Path
    import tempfile

    import pydicom
    from pydicom.dataset import Dataset, FileMetaDataset
    from pydicom.uid import UID, ExplicitVRLittleEndian


    print("Setting dataset values...")
    ds = Dataset()
    ds.PatientName = "Test^Firstname"
    ds.PatientID = "123456"
    # Set creation date/time
    ds.ContentDate = "20000101"

    print("Setting file meta information...")
    # Populate required values for file meta information
    file_meta = FileMetaDataset()
    file_meta.MediaStorageSOPClassUID = UID("1.2.840.10008.5.1.4.1.1.2")
    file_meta.MediaStorageSOPInstanceUID = UID("1.2.3")
    file_meta.ImplementationClassUID = UID("1.2.3.4")
    file_meta.TransferSyntaxUID = ExplicitVRLittleEndian

    # Add the file meta information
    ds.file_meta = file_meta

    path = Path(tempfile.NamedTemporaryFile(suffix=".dcm").name)
    print(f"Writing dataset to tempfile")
    ds.save_as(path, enforce_file_format=True)

    # reopen the data just for checking
    print(f"Load dataset from: tempfile ...")
    ds = pydicom.dcmread(path)
    print(ds)

    # remove the created file
    print(f"Deleting file: tempfile ...")
    path.unlink()


.. _sphx_glr_download_auto_examples_input_output_plot_write_dicom.py:

.. only:: html

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

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

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

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

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

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

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


.. only:: html

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

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