.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "11_demos\itom\plots\demo_maskEditor.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_plots_demo_maskEditor.py: Mask editor ============== This demo shows a mask editor where a masked ``dataObject`` can be created from shapes that are drawn on the plot canvas. The result of such shapes is a list of itom.shape objects. These are sub-pixel precise geometric shapes. They can then be converted to pixel-precise masks. For more information about shapes see the shapes demo in the main folder of the demo scripts. .. GENERATED FROM PYTHON SOURCE LINES 10-17 .. code-block:: Python from itom import ui from itom import dataObject from itom import algorithms from itom import shape .. GENERATED FROM PYTHON SOURCE LINES 19-20 Controls if the user can add or modify shapes by the actions in the toolbar .. GENERATED FROM PYTHON SOURCE LINES 20-24 .. code-block:: Python def checkEnableDrawingClicked(checked: bool): gui.plot["geometricShapesDrawingEnabled"] = checked .. GENERATED FROM PYTHON SOURCE LINES 25-26 Clear all existing shapes by calling the slot ``clearGeometricShapes`` .. GENERATED FROM PYTHON SOURCE LINES 26-30 .. code-block:: Python def clearShapes(): gui.plot.call("clearGeometricShapes") .. GENERATED FROM PYTHON SOURCE LINES 31-36 Creates a mask where all values within the mask are set to ``255`` and all other values to ``0``. The mask has the same size and axes information than the displayed ``dataObject``. The mask object is then set to the variable ``mask`` in the global workspace and displayed in a new plot window. .. GENERATED FROM PYTHON SOURCE LINES 36-42 .. code-block:: Python def exportMask(): mask = gui.plot["source"].createMask(gui.plot["geometricShapes"]) globals()["mask"] = mask plot(mask, properties={"title": "exported mask"}) .. GENERATED FROM PYTHON SOURCE LINES 43-44 Exports all current shapes as a list of itom.shape objects. .. GENERATED FROM PYTHON SOURCE LINES 44-53 .. code-block:: Python def exportShapes(): globals()["shapes"] = gui.plot["geometricShapes"] ui.msgInformation( "shapes exported", "shapes exported to the workspace under the variable 'shapes': \n" + str(globals()["shapes"]), ) .. GENERATED FROM PYTHON SOURCE LINES 54-56 Show the mask object as overlay image. Use the slider in the toolbox of the plot to change the transparency of the overlay image. .. GENERATED FROM PYTHON SOURCE LINES 56-61 .. code-block:: Python def showMaskAsOverlay(): mask = gui.plot["source"].createMask(gui.plot["geometricShapes"]) gui.plot["overlayImage"] = mask .. GENERATED FROM PYTHON SOURCE LINES 62-63 Sets all values within any shapes to a given gray value. .. GENERATED FROM PYTHON SOURCE LINES 63-79 .. code-block:: Python def setColorUnderMask(): [val, ok] = ui.getInt( "value", "set the value for all values within the mask:", 128, 0, 255 ) if ok: mask = gui.plot["source"].createMask(gui.plot["geometricShapes"]) # the mask can be inverted using ~mask gui.plot["source"][mask] = ( val # this single command can be used to change values in the mask ) gui.plot.call( "replot" ) # if only the source object is changed, you need to call # ``replot`` such that the plot is updated, too. .. GENERATED FROM PYTHON SOURCE LINES 80-82 This method is always called if any shape is added or modified and displays some information in the status bar of the window. .. GENERATED FROM PYTHON SOURCE LINES 82-88 .. code-block:: Python def shapeModified(index: int, shape: shape): gui.call("statusBar").call( "showMessage", "Shape %i modified: %s" % (index, str(shape)), 1000 ) .. GENERATED FROM PYTHON SOURCE LINES 89-90 This method is called if the user changes the selection of allowed operations. .. GENERATED FROM PYTHON SOURCE LINES 90-100 .. code-block:: Python def listModificationChanged(): sel = gui.listModificationTypes.call("selectedRows") sel2 = [] if 0 in sel: sel2.append("Move") if 1 in sel: sel2.append("Resize") gui.plot["geometryModificationModes"] = ";".join(sel2) .. GENERATED FROM PYTHON SOURCE LINES 101-103 Create demo data with axis scales and offsets to show that the mask will also work in this special case. .. GENERATED FROM PYTHON SOURCE LINES 103-128 .. code-block:: Python image = dataObject.randN([1024, 1024]) image.axisScales = (1e-3, 1e-3) image.axisOffsets = (512, 512) image.axisUnits = ("mm", "mm") image.axisDescriptions = ("y", "x") image.valueUnit = "a.u." image.valueDescription = "intensity" algorithms.lowPassFilter(image, image, 7, 7) gui = ui("demo_MaskEditor.ui", ui.TYPEWINDOW) # connect signal-slots gui.checkEnableDrawing.connect("toggled(bool)", checkEnableDrawingClicked) gui.btnExportMask.connect("clicked()", exportMask) gui.btnExportShape.connect("clicked()", exportShapes) gui.btnShowMaskOverlay.connect("clicked()", showMaskAsOverlay) gui.btnSetColorUnderMask.connect("clicked()", setColorUnderMask) gui.btnClearShapes.connect("clicked()", clearShapes) gui.plot.connect("geometricShapeChanged(int,ito::Shape)", shapeModified) gui.listModificationTypes.connect("itemSelectionChanged()", listModificationChanged) gui.plot["source"] = image gui.plot["colorMap"] = "hotIron" gui.listModificationTypes.call("selectRows", (0, 1)) gui.show() .. GENERATED FROM PYTHON SOURCE LINES 129-131 .. image:: ../../_static/demoMaskEditor.png :width: 100% .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.042 seconds) .. _sphx_glr_download_11_demos_itom_plots_demo_maskEditor.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: demo_maskEditor.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: demo_maskEditor.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: demo_maskEditor.zip `