.. 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 Click :ref:`here ` 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 6-14 .. code-block:: default 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 16-17 Create a colored dataObject of type ``rgba32``. .. GENERATED FROM PYTHON SOURCE LINES 17-20 .. code-block:: default rgba32 = dataObject([100, 100], "rgba32") .. GENERATED FROM PYTHON SOURCE LINES 21-24 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 24-33 .. code-block:: default rgba32[0:100, 0:100] = rgba(150, 150, 150, 255) """insert a red, green and blue bar in the picture wich 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 (108, PlotItem(UiItem(class: Itom2dQwtPlot, name: plot0x0))) .. GENERATED FROM PYTHON SOURCE LINES 34-35 Save the ``dataObject`` as a *.tiff file with a rgba color palette. .. GENERATED FROM PYTHON SOURCE LINES 35-37 .. code-block:: default algorithms.saveTiff(rgba32, "pic_rgba.tiff", "rgba") .. GENERATED FROM PYTHON SOURCE LINES 38-39 Reload the picture as it was, that is of type ``rgba32``. .. GENERATED FROM PYTHON SOURCE LINES 39-42 .. code-block:: default reload_tiff_rgba = dataObject() algorithms.loadAnyImage(reload_tiff_rgba, "pic_rgba.tiff", "asIs") .. GENERATED FROM PYTHON SOURCE LINES 43-47 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 choosen as color palette the colored ``dataObject`` will be converted to a gray image .. GENERATED FROM PYTHON SOURCE LINES 47-49 .. code-block:: default algorithms.saveTiff(rgba32, "pic_rgb.tiff", "rgb") .. GENERATED FROM PYTHON SOURCE LINES 50-52 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 52-55 .. code-block:: default reload_tiff_rgb = dataObject() algorithms.loadAnyImage(reload_tiff_rgb, "pic_rgb.tiff", "asIs") .. GENERATED FROM PYTHON SOURCE LINES 56-58 Save the ``dataObject`` as a *.png file with a ``gray`` color palette (also ``gray16`` and all colored palettes are supportted). .. GENERATED FROM PYTHON SOURCE LINES 58-60 .. code-block:: default algorithms.savePNG(rgba32, "pic_gray.png", "gray") .. GENERATED FROM PYTHON SOURCE LINES 61-62 Reload the picture as it was, that is of type ``gray`` (type ``uint8``) .. GENERATED FROM PYTHON SOURCE LINES 62-65 .. code-block:: default reload_png_gray = dataObject() algorithms.loadAnyImage(reload_png_gray, "pic_gray.png", "asIs") .. GENERATED FROM PYTHON SOURCE LINES 66-68 Save the ``dataObject`` as a *.pgm with a 16bit grayscale (``gray`` and ``gray16`` are only supported for gray images). .. GENERATED FROM PYTHON SOURCE LINES 68-70 .. code-block:: default algorithms.savePGM(rgba32, "pic_gray.pgm", "gray16") .. GENERATED FROM PYTHON SOURCE LINES 71-73 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 73-76 .. code-block:: default reload_pgm_gray16 = dataObject() algorithms.loadAnyImage(reload_pgm_gray16, "pic_gray.pgm", "asIs") .. GENERATED FROM PYTHON SOURCE LINES 77-79 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 79-82 .. code-block:: default dataDict = {"data": rgba32} saveIDC("pic_idc.idc", dataDict) .. GENERATED FROM PYTHON SOURCE LINES 83-84 Load the *.idc file as it was, that is of type ``dictionary``. .. GENERATED FROM PYTHON SOURCE LINES 84-87 .. code-block:: default loaded_dic = loadIDC("pic_idc.idc") reload_img = loaded_dic["data"] .. GENERATED FROM PYTHON SOURCE LINES 88-89 Copy the dataObject .. GENERATED FROM PYTHON SOURCE LINES 89-91 .. code-block:: default rgba32_1 = rgba32 .. GENERATED FROM PYTHON SOURCE LINES 92-93 Save both (also more possible) in one *.idc file. .. GENERATED FROM PYTHON SOURCE LINES 93-96 .. code-block:: default dic_1 = {"data_1": rgba32, "data_2": rgba32_1} loaded_dic_1 = saveIDC("multi_pic_idc.idc", dic_1) .. GENERATED FROM PYTHON SOURCE LINES 97-99 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 99-107 .. code-block:: default 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 108-110 Save as *.tiff file colored in the ``hotIron`` color palette. Other palettes are for example ``grayMarked`` or ``falseColor``. .. GENERATED FROM PYTHON SOURCE LINES 110-112 .. code-block:: default algorithms.saveTiff(uint8, "pic_uint8.tiff", "hotIron") .. GENERATED FROM PYTHON SOURCE LINES 113-115 This section shows how to save floating point ``dataObjects`` as a image. create a gray image of type float32 .. GENERATED FROM PYTHON SOURCE LINES 115-123 .. code-block:: default 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 124-129 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 129-131 .. code-block:: default algorithms.savePNG(float32, "pic_falseColor.png", "hotIron") .. GENERATED FROM PYTHON SOURCE LINES 132-134 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 134-137 .. code-block:: default reload_png_falseColor = dataObject() algorithms.loadAnyImage(reload_png_falseColor, "pic_falseColor.png", "GRAY") .. GENERATED FROM PYTHON SOURCE LINES 138-140 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 140-143 .. code-block:: default normfloat32 = float32.normalize(0.0, 1.0, "float32") algorithms.savePNG(normfloat32, "pic_normalized_falseColor.png", "hotIron") .. GENERATED FROM PYTHON SOURCE LINES 144-146 Reload the image as a ``uint8`` ``dataObject`` ->all steps are included. .. GENERATED FROM PYTHON SOURCE LINES 146-151 .. code-block:: default 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.185 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-python :download:`Download Python source code: demo_LoadSaveDataObjects.py ` .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: demo_LoadSaveDataObjects.ipynb `