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

    Click :ref:`here <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.






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

 Out:

 .. code-block:: none

    Setting file meta information...
    Setting dataset values...
    Writing test file /tmp/tmprdvqwhpd.dcm
    File saved.
    Writing test file as Big Endian Explicit VR /tmp/tmpd62owe24.dcm
    Load file /tmp/tmprdvqwhpd.dcm ...
    (0008, 0023) Content Date                        DA: '20181123'
    (0008, 0033) Content Time                        TM: '224037.575733'
    (0010, 0010) Patient's Name                      PN: 'Test^Firstname'
    (0010, 0020) Patient ID                          LO: '123456'
    Remove file /tmp/tmprdvqwhpd.dcm ...
    Load file /tmp/tmpd62owe24.dcm ...
    (0008, 0023) Content Date                        DA: '20181123'
    (0008, 0033) Content Time                        TM: '224037.575733'
    (0010, 0010) Patient's Name                      PN: 'Test^Firstname'
    (0010, 0020) Patient ID                          LO: '123456'
    Remove file /tmp/tmpd62owe24.dcm ...




|


.. code-block:: python


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

    import os
    import tempfile
    import datetime

    import pydicom
    from pydicom.dataset import Dataset, FileDataset

    # Create some temporary filenames
    suffix = '.dcm'
    filename_little_endian = tempfile.NamedTemporaryFile(suffix=suffix).name
    filename_big_endian = tempfile.NamedTemporaryFile(suffix=suffix).name

    print("Setting file meta information...")
    # Populate required values for file meta information
    file_meta = Dataset()
    file_meta.MediaStorageSOPClassUID = '1.2.840.10008.5.1.4.1.1.2'
    file_meta.MediaStorageSOPInstanceUID = "1.2.3"
    file_meta.ImplementationClassUID = "1.2.3.4"

    print("Setting dataset values...")
    # Create the FileDataset instance (initially no data elements, but file_meta
    # supplied)
    ds = FileDataset(filename_little_endian, {},
                     file_meta=file_meta, preamble=b"\0" * 128)

    # Add the data elements -- not trying to set all required here. Check DICOM
    # standard
    ds.PatientName = "Test^Firstname"
    ds.PatientID = "123456"

    # Set the transfer syntax
    ds.is_little_endian = True
    ds.is_implicit_VR = True

    # Set creation date/time
    dt = datetime.datetime.now()
    ds.ContentDate = dt.strftime('%Y%m%d')
    timeStr = dt.strftime('%H%M%S.%f')  # long format with micro seconds
    ds.ContentTime = timeStr

    print("Writing test file", filename_little_endian)
    ds.save_as(filename_little_endian)
    print("File saved.")

    # Write as a different transfer syntax XXX shouldn't need this but pydicom
    # 0.9.5 bug not recognizing transfer syntax
    ds.file_meta.TransferSyntaxUID = pydicom.uid.ExplicitVRBigEndian
    ds.is_little_endian = False
    ds.is_implicit_VR = False

    print("Writing test file as Big Endian Explicit VR", filename_big_endian)
    ds.save_as(filename_big_endian)

    # reopen the data just for checking
    for filename in (filename_little_endian, filename_big_endian):
        print('Load file {} ...'.format(filename))
        ds = pydicom.dcmread(filename)
        print(ds)

        # remove the created file
        print('Remove file {} ...'.format(filename))
        os.remove(filename)

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


.. _sphx_glr_download_auto_examples_input_output_plot_write_dicom.py:


.. only :: html

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



  .. container:: sphx-glr-download

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



  .. container:: sphx-glr-download

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


.. only:: html

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

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