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

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

.. _sphx_glr_auto_examples_memory_dataset.py:


======================================
Show working with memory-based dataset
======================================

Show how to write a DICOM dataset into a byte array and to read
it back from a byte array. This can be helpful for example if working with
datasets saved as blobs in a database.

.. GENERATED FROM PYTHON SOURCE LINES 12-86

.. code-block:: Python



    from io import BytesIO

    from pydicom import dcmread, dcmwrite, Dataset
    from pydicom.filebase import DicomFileLike

    print(__doc__)

    usage = "Usage: python memory_dataset.py dicom_filename"


    def write_dataset_to_bytes(ds: Dataset) -> bytes:
        # create a buffer
        with BytesIO() as buffer:
            # create a DicomFileLike object that has some properties of DataSet
            memory_dataset = DicomFileLike(buffer)
            # write the dataset to the DicomFileLike object
            dcmwrite(memory_dataset, ds)
            # to read from the object, you have to rewind it
            memory_dataset.seek(0)
            # read the contents as bytes
            return memory_dataset.read()


    def adapt_dataset_from_bytes(blob: bytes) -> Dataset:
        # you can just read the dataset from the byte array
        dataset = dcmread(BytesIO(blob))
        # do some interesting stuff
        dataset.is_little_endian = False
        dataset.PatientName = "Bond^James"
        dataset.PatientID = "007"
        return dataset


    class DummyDataBase:
        def __init__(self) -> None:
            self._blobs: dict[str, bytes] = {}

        def save(self, name: str, blob: bytes) -> None:
            self._blobs[name] = blob

        def load(self, name: str) -> bytes | None:
            return self._blobs.get(name)


    if __name__ == "__main__":
        import sys

        if len(sys.argv) != 2:
            print("Please supply a dicom file name:\n")
            print(usage)
            sys.exit(-1)
        file_path = sys.argv[1]
        db = DummyDataBase()

        # Convert a dataset to a byte array:
        # - read the dataset from a file
        dataset = dcmread(file_path)
        print(dataset)
        # - convert the dataset to bytes
        ds_bytes = write_dataset_to_bytes(dataset)
        # - save the bytes in some storage
        db.save("dataset", ds_bytes)

        # Convert a byte array to a dataset:
        # - get the bytes from storage
        read_bytes = db.load("dataset")
        if read_bytes:
            # - convert the bytes into a dataset and do something interesting with it
            read_dataset = adapt_dataset_from_bytes(read_bytes)
            print(read_dataset)
            # - you can write your dataset to a file if wanted
            dcmwrite(file_path + "_new", read_dataset)


.. _sphx_glr_download_auto_examples_memory_dataset.py:

.. only:: html

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

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

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

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

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

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

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


.. only:: html

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

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