.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "11_demos\itom\basics\demo_LoadSaveDataObjects.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note :ref:`Go to the end ` to download the full example code. .. rst-class:: sphx-glr-example-title .. _sphx_glr_11_demos_itom_basics_demo_LoadSaveDataObjects.py: Load and save dataObject ========================= This demo shows how to **save** and **load** ``dataObjects`` to/from image formats as well as native itom formats. .. GENERATED FROM PYTHON SOURCE LINES 7-15 .. code-block:: Python from itom import dataObject from itom import algorithms from itom import rgba from itom import plot from itom import saveIDC from itom import loadIDC .. GENERATED FROM PYTHON SOURCE LINES 17-18 Create a colored dataObject of type ``rgba32``. .. GENERATED FROM PYTHON SOURCE LINES 18-21 .. code-block:: Python rgba32 = dataObject([100, 100], "rgba32") .. GENERATED FROM PYTHON SOURCE LINES 22-25 Set all pixels to a gray value. Therefore ``red=green=blue`` with no transparency, what means that alpha has to be set to the maximal value of ``255``. .. GENERATED FROM PYTHON SOURCE LINES 25-34 .. code-block:: Python rgba32[0:100, 0:100] = rgba(150, 150, 150, 255) """insert a red, green and blue bar in the picture which are not complete intransparent""" rgba32[10:30, :] = rgba(255, 0, 0, 150) rgba32[50:70, :] = rgba(0, 255, 0, 150) rgba32[80:100, :] = rgba(0, 0, 255, 150) """show the image""" plot(rgba32) .. rst-class:: sphx-glr-script-out .. code-block:: none (109, PlotItem(UiItem(class: Itom2dQwtPlot, name: plot0x0))) .. GENERATED FROM PYTHON SOURCE LINES 35-36 Save the ``dataObject`` as a `*.tiff` file with a rgba color palette. .. GENERATED FROM PYTHON SOURCE LINES 36-38 .. code-block:: Python algorithms.saveTiff(rgba32, "pic_rgba.tiff", "rgba") .. GENERATED FROM PYTHON SOURCE LINES 39-40 Reload the picture as it was, that is of type ``rgba32``. .. GENERATED FROM PYTHON SOURCE LINES 40-43 .. code-block:: Python reload_tiff_rgba = dataObject() algorithms.loadAnyImage(reload_tiff_rgba, "pic_rgba.tiff", "asIs") .. GENERATED FROM PYTHON SOURCE LINES 44-48 Save the ``dataObject`` as a `*.tiff` file with a rgb color palette, which causes that the transparency of the bars will be ignored. If ``gray`` or ``gray16`` is chosen as color palette the colored ``dataObject`` will be converted to a gray image .. GENERATED FROM PYTHON SOURCE LINES 48-50 .. code-block:: Python algorithms.saveTiff(rgba32, "pic_rgb.tiff", "rgb") .. GENERATED FROM PYTHON SOURCE LINES 51-53 Reload the picture as it was, that is of type ``rgba32`` with all alpha values set to ``255`` (no transparency). .. GENERATED FROM PYTHON SOURCE LINES 53-56 .. code-block:: Python reload_tiff_rgb = dataObject() algorithms.loadAnyImage(reload_tiff_rgb, "pic_rgb.tiff", "asIs") .. GENERATED FROM PYTHON SOURCE LINES 57-59 Save the ``dataObject`` as a `*.png` file with a ``gray`` color palette (also ``gray16`` and all colored palettes are supported). .. GENERATED FROM PYTHON SOURCE LINES 59-61 .. code-block:: Python algorithms.savePNG(rgba32, "pic_gray.png", "gray") .. GENERATED FROM PYTHON SOURCE LINES 62-63 Reload the picture as it was, that is of type ``gray`` (type ``uint8``) .. GENERATED FROM PYTHON SOURCE LINES 63-66 .. code-block:: Python reload_png_gray = dataObject() algorithms.loadAnyImage(reload_png_gray, "pic_gray.png", "asIs") .. GENERATED FROM PYTHON SOURCE LINES 67-69 Save the ``dataObject`` as a `*.pgm` with a 16bit grayscale (``gray`` and ``gray16`` are only supported for gray images). .. GENERATED FROM PYTHON SOURCE LINES 69-71 .. code-block:: Python algorithms.savePGM(rgba32, "pic_gray.pgm", "gray16") .. GENERATED FROM PYTHON SOURCE LINES 72-74 Load the `*.pgm` file as it was, that is of type ``gray`` (type ``uint16`` due to the 16bit gray color palette) .. GENERATED FROM PYTHON SOURCE LINES 74-77 .. code-block:: Python reload_pgm_gray16 = dataObject() algorithms.loadAnyImage(reload_pgm_gray16, "pic_gray.pgm", "asIs") .. GENERATED FROM PYTHON SOURCE LINES 78-80 Save the ``dataObject`` as an `*.idc` file (itom data collection, saved using Python module ``pickle``) therefore it must be wrapped into a ``dictionary``. .. GENERATED FROM PYTHON SOURCE LINES 80-83 .. code-block:: Python dataDict = {"data": rgba32} saveIDC("pic_idc.idc", dataDict) .. GENERATED FROM PYTHON SOURCE LINES 84-85 Load the `*.idc` file as it was, that is of type ``dictionary``. .. GENERATED FROM PYTHON SOURCE LINES 85-88 .. code-block:: Python loaded_dic = loadIDC("pic_idc.idc") reload_img = loaded_dic["data"] .. GENERATED FROM PYTHON SOURCE LINES 89-90 Copy the dataObject .. GENERATED FROM PYTHON SOURCE LINES 90-92 .. code-block:: Python rgba32_1 = rgba32 .. GENERATED FROM PYTHON SOURCE LINES 93-94 Save both (also more possible) in one `*.idc` file. .. GENERATED FROM PYTHON SOURCE LINES 94-97 .. code-block:: Python dic_1 = {"data_1": rgba32, "data_2": rgba32_1} loaded_dic_1 = saveIDC("multi_pic_idc.idc", dic_1) .. GENERATED FROM PYTHON SOURCE LINES 98-100 In this section a ``uint8`` ``dataObject`` is created and saved in false colors. create a gray image of type uint8 .. GENERATED FROM PYTHON SOURCE LINES 100-108 .. code-block:: Python uint8 = dataObject([100, 100], "uint8") # insert blocks with values of 0.0, 1.0, 50 and 100 uint8[0:25, :] = 0 uint8[25:50, :] = 1 uint8[50:75, :] = 50 uint8[75:100, :] = 100 .. GENERATED FROM PYTHON SOURCE LINES 109-111 Save as `*.tiff` file colored in the ``hotIron`` color palette. Other palettes are for example ``grayMarked`` or ``falseColor``. .. GENERATED FROM PYTHON SOURCE LINES 111-113 .. code-block:: Python algorithms.saveTiff(uint8, "pic_uint8.tiff", "hotIron") .. GENERATED FROM PYTHON SOURCE LINES 114-116 This section shows how to save floating point ``dataObjects`` as a image. create a gray image of type float32 .. GENERATED FROM PYTHON SOURCE LINES 116-124 .. code-block:: Python float32 = dataObject([100, 100], "float32") # insert blocks with values of 0.0, 1.0, 50 and 100 float32[0:25, :] = 0.0 float32[25:50, :] = 1.0 float32[50:75, :] = 50.0 float32[75:100, :] = 100.0 .. GENERATED FROM PYTHON SOURCE LINES 125-130 Save the ``float32`` ``dataObject`` as a `*.png` file with a ``falseColor`` palette (here ``hotIron`` is used, others are for example ``grayMarked`` or ``falseColor``). If you save a ``dataObject`` of type float the color palette is spaced between ``[0, 1]`` ->all values above ``1.0`` will be clipped to the maximum value. .. GENERATED FROM PYTHON SOURCE LINES 130-132 .. code-block:: Python algorithms.savePNG(float32, "pic_falseColor.png", "hotIron") .. GENERATED FROM PYTHON SOURCE LINES 133-135 Reload the saved `*.png` as a ``uint8`` ``dataObject`` ->all steps with values above 1.0 have the same gray value. .. GENERATED FROM PYTHON SOURCE LINES 135-138 .. code-block:: Python reload_png_falseColor = dataObject() algorithms.loadAnyImage(reload_png_falseColor, "pic_falseColor.png", "GRAY") .. GENERATED FROM PYTHON SOURCE LINES 139-141 To get rid of the problem above you need to normalize your ``dataObject`` between ``0.0`` and ``1.0`` using the function ``normalize``. .. GENERATED FROM PYTHON SOURCE LINES 141-144 .. code-block:: Python normfloat32 = float32.normalize(0.0, 1.0, "float32") algorithms.savePNG(normfloat32, "pic_normalized_falseColor.png", "hotIron") .. GENERATED FROM PYTHON SOURCE LINES 145-147 Reload the image as a ``uint8`` ``dataObject`` ->all steps are included. .. GENERATED FROM PYTHON SOURCE LINES 147-153 .. code-block:: Python reload_normalized_falseColor = dataObject() algorithms.loadAnyImage( reload_normalized_falseColor, "pic_normalized_falseColor.png", "GRAY", ) .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.126 seconds) .. _sphx_glr_download_11_demos_itom_basics_demo_LoadSaveDataObjects.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: demo_LoadSaveDataObjects.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: demo_LoadSaveDataObjects.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: demo_LoadSaveDataObjects.zip `