{ "cells": [ { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "%matplotlib inline" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Load and save dataObject\n\nThis demo shows how to **save** and **load** ``dataObjects``\nto/from image formats as well as native itom formats.\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "from itom import dataObject\nfrom itom import algorithms\nfrom itom import rgba\nfrom itom import plot\nfrom itom import saveIDC\nfrom itom import loadIDC" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Create a colored dataObject of type ``rgba32``.\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "rgba32 = dataObject([100, 100], \"rgba32\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Set all pixels to a gray value.\nTherefore ``red=green=blue`` with no transparency,\nwhat means that alpha has to be set to the maximal value of ``255``.\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "rgba32[0:100, 0:100] = rgba(150, 150, 150, 255)\n\n\"\"\"insert a red, green and blue bar in the picture wich are not complete intransparent\"\"\"\nrgba32[10:30, :] = rgba(255, 0, 0, 150)\nrgba32[50:70, :] = rgba(0, 255, 0, 150)\nrgba32[80:100, :] = rgba(0, 0, 255, 150)\n\"\"\"show the image\"\"\"\nplot(rgba32)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Save the ``dataObject`` as a *.tiff file with a rgba color palette.\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "algorithms.saveTiff(rgba32, \"pic_rgba.tiff\", \"rgba\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Reload the picture as it was, that is of type ``rgba32``.\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "reload_tiff_rgba = dataObject()\nalgorithms.loadAnyImage(reload_tiff_rgba, \"pic_rgba.tiff\", \"asIs\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Save the ``dataObject`` as a *.tiff file with a rgb color palette,\nwhich causes that the transparency of the bars will be ignored. \nIf ``gray`` or ``gray16`` is choosen as color palette the colored\n``dataObject`` will be converted to a gray image \n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "algorithms.saveTiff(rgba32, \"pic_rgb.tiff\", \"rgb\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Reload the picture as it was, that is of type ``rgba32``\nwith all alpha values set to ``255`` (no transparency).\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "reload_tiff_rgb = dataObject()\nalgorithms.loadAnyImage(reload_tiff_rgb, \"pic_rgb.tiff\", \"asIs\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Save the ``dataObject`` as a *.png file with a ``gray`` color palette\n(also ``gray16`` and all colored palettes are supportted).\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "algorithms.savePNG(rgba32, \"pic_gray.png\", \"gray\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Reload the picture as it was, that is of type ``gray`` (type ``uint8``) \n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "reload_png_gray = dataObject()\nalgorithms.loadAnyImage(reload_png_gray, \"pic_gray.png\", \"asIs\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Save the ``dataObject`` as a *.pgm with a 16bit grayscale\n(``gray`` and ``gray16`` are only supported for gray images).\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "algorithms.savePGM(rgba32, \"pic_gray.pgm\", \"gray16\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Load the *.pgm file as it was, that is of type ``gray``\n(type ``uint16`` due to the 16bit gray color palette) \n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "reload_pgm_gray16 = dataObject()\nalgorithms.loadAnyImage(reload_pgm_gray16, \"pic_gray.pgm\", \"asIs\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Save the ``dataObject`` as an *.idc file (itom data collection,\nsaved using Python module ``pickle``) therefore it must be wrapped into a ``dictionary``.\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "dataDict = {\"data\": rgba32}\nsaveIDC(\"pic_idc.idc\", dataDict)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Load the *.idc file as it was, that is of type ``dictionary``.\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "loaded_dic = loadIDC(\"pic_idc.idc\")\nreload_img = loaded_dic[\"data\"]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Copy the dataObject\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "rgba32_1 = rgba32" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Save both (also more possible) in one *.idc file.\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "dic_1 = {\"data_1\": rgba32, \"data_2\": rgba32_1}\nloaded_dic_1 = saveIDC(\"multi_pic_idc.idc\", dic_1)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "In this section a ``uint8`` ``dataObject`` is created and saved in false colors.\ncreate a gray image of type uint8\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "uint8 = dataObject([100, 100], \"uint8\")\n\n# insert blocks with values of 0.0, 1.0, 50 and 100\nuint8[0:25, :] = 0\nuint8[25:50, :] = 1\nuint8[50:75, :] = 50\nuint8[75:100, :] = 100" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Save as *.tiff file colored in the ``hotIron`` color palette.\nOther palettes are for example ``grayMarked`` or ``falseColor``.\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "algorithms.saveTiff(uint8, \"pic_uint8.tiff\", \"hotIron\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "This section shows how to save floating point ``dataObjects`` as a image.\ncreate a gray image of type float32\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "float32 = dataObject([100, 100], \"float32\")\n\n# insert blocks with values of 0.0, 1.0, 50 and 100\nfloat32[0:25, :] = 0.0\nfloat32[25:50, :] = 1.0\nfloat32[50:75, :] = 50.0\nfloat32[75:100, :] = 100.0" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Save the ``float32`` ``dataObject`` as a *.png file\nwith a ``falseColor`` palette (here ``hotIron`` is used,\nothers are for example ``grayMarked`` or ``falseColor``).\nIf you save a ``dataObject`` of type float the color palette is spaced between\n``[0, 1]`` ->all values above ``1.0`` will be clipped to the maximum value.\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "algorithms.savePNG(float32, \"pic_falseColor.png\", \"hotIron\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Reload the saved *.png as a ``uint8`` ``dataObject``\n->all steps with values above 1.0 have the same gray value.\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "reload_png_falseColor = dataObject()\nalgorithms.loadAnyImage(reload_png_falseColor, \"pic_falseColor.png\", \"GRAY\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "To get rid of the problem above you need to normalize your\n``dataObject`` between ``0.0`` and ``1.0`` using the function ``normalize``.\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "normfloat32 = float32.normalize(0.0, 1.0, \"float32\")\nalgorithms.savePNG(normfloat32, \"pic_normalized_falseColor.png\", \"hotIron\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Reload the image as a ``uint8`` ``dataObject``\n->all steps are included.\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "reload_normalized_falseColor = dataObject()\nalgorithms.loadAnyImage(\n reload_normalized_falseColor,\n \"pic_normalized_falseColor.png\",\n \"GRAY\",\n)" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.8.10" } }, "nbformat": 4, "nbformat_minor": 0 }