OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

Seeing image dimensions in silx viewer Plot2D

  • Thread starter Thread starter Marco Montevechi Filho
  • Start date Start date
M

Marco Montevechi Filho

Guest
I am using a custom silx viewer to visualize a series of hdf5 imagens stored in a single file. The default silx viewer Plot2D behaviour lets you browse trough images with a slider:

enter image description here

I want to apply a custom PlotAction that modifies the image. It works with (complete code at the end of question):

Code:
class SumAction(PlotAction):
    def __init__(self, plot, parent=None):
    
    sum_icon = qt.QIcon("./matrix.png")
    
    super(SumAction, self).__init__(
        plot, icon=sum_icon, text='Sum',
        tooltip='Sum 100 to each image pixel.',
        triggered=self.__sum,
        checkable=True, parent=parent)

def __sum(self, checked):

    activeImage = self.plot.getActiveImage()

    if activeImage is None:
        qt.QMessageBox.information(
            self.plot,
            "Plot data is None. Open a valid 2D image to use this function."
        )
        
    data = activeImage.getData()
        
    self.originalData = activeImage.getData()
    data+=100
    activeImage.setData(data)

The thing is: whenever i slide right or left, the modification is gone. Having as premise that the best way to make this persistent is to override the slider function to call my custom action if my action button is checked (I could be wrong), i want to do something like:

Code:
def slideToImage(image_number):
    super(ActualSlider, self).slideToImage()
    if cool_button == checked:
        do_things_to_image()

So that my changes seem persistent. But i couldnt find anything about how to override the slider function. Is there a way to do that?

Complete code:

Code:
#!/usr/bin/env python

import sys
import numpy

from silx.gui import qt

def createWindow(parent, settings):
    # Local import to avoid early import (like h5py)
    #SOme libraries have to be configured first properly
    from silx.gui.plot.actions import PlotAction
    from silx.gui.widgets.FrameBrowser import FrameBrowser
    from silx.app.view.Viewer import Viewer
    from silx.app.view.ApplicationContext import ApplicationContext
        
    class SumAction(PlotAction):
        def __init__(self, plot, parent=None):
            
            sum_icon = qt.QIcon("./matrix.png")
            
            super(SumAction, self).__init__(
                plot, icon=sum_icon, text='Sum',
                tooltip='Sum 100 to each image pixel.',
                triggered=self.__sum,
                checkable=True, parent=parent)

        def __sum(self, checked):

            activeImage = self.plot.getActiveImage()

            if activeImage is None:
                qt.QMessageBox.information(
                    self.plot,
                    "Plot data is None. Open a valid 2D image to use this function."
                )
                
            data = activeImage.getData()
                
            self.originalData = activeImage.getData()
            data+=100
            activeImage.setData(data)
                            
    class MyApplicationContext(ApplicationContext):
        """This class is shared to all the silx view application."""
    
        def findPrintToolBar(self, plot):
            # FIXME: It would be better to use the Qt API
            return plot._outputToolBar

        def viewWidgetCreated(self, view, widget):
            """Called when the widget of the view was created.

            So we can custom it.
            """
            from silx.gui.plot import Plot2D
            if isinstance(widget, Plot2D):
                toolBar = self.findPrintToolBar(widget)
                action = SumAction(widget, widget)
                toolBar.addAction(action)

    class MyViewer(Viewer):
        def createApplicationContext(self, settings):
            return MyApplicationContext(self, settings)

    window = MyViewer(parent=parent, settings=settings)
    window.setWindowTitle(window.windowTitle() + " [custom]")
    return window


def main(args):
    from silx.app.view import main as silx_view_main
    # Monkey patch the main window creation
    silx_view_main.createWindow = createWindow
    # Use the default launcher
    silx_view_main.main(args)


if __name__ == '__main__':
    main(sys.argv)
<p>I am using a custom silx viewer to visualize a series of hdf5 imagens stored in a single file. The default silx viewer Plot2D behaviour lets you browse trough images with a slider:</p>
<p><a href="https://i.sstatic.net/YtDfY.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/YtDfY.png" alt="enter image description here" /></a></p>
<p>I want to apply a custom PlotAction that modifies the image. It works with (complete code at the end of question):</p>
<pre><code>class SumAction(PlotAction):
def __init__(self, plot, parent=None):

sum_icon = qt.QIcon("./matrix.png")

super(SumAction, self).__init__(
plot, icon=sum_icon, text='Sum',
tooltip='Sum 100 to each image pixel.',
triggered=self.__sum,
checkable=True, parent=parent)

def __sum(self, checked):

activeImage = self.plot.getActiveImage()

if activeImage is None:
qt.QMessageBox.information(
self.plot,
"Plot data is None. Open a valid 2D image to use this function."
)

data = activeImage.getData()

self.originalData = activeImage.getData()
data+=100
activeImage.setData(data)
</code></pre>
<p>The thing is: whenever i slide right or left, the modification is gone. Having as premise that the best way to make this persistent is to override the slider function to call my custom action if my action button is checked (I could be wrong), i want to do something like:</p>
<pre><code>def slideToImage(image_number):
super(ActualSlider, self).slideToImage()
if cool_button == checked:
do_things_to_image()
</code></pre>
<p>So that my changes seem persistent. But i couldnt find anything about how to override the slider function. Is there a way to do that?</p>
<p>Complete code:</p>
<pre><code>#!/usr/bin/env python

import sys
import numpy

from silx.gui import qt

def createWindow(parent, settings):
# Local import to avoid early import (like h5py)
#SOme libraries have to be configured first properly
from silx.gui.plot.actions import PlotAction
from silx.gui.widgets.FrameBrowser import FrameBrowser
from silx.app.view.Viewer import Viewer
from silx.app.view.ApplicationContext import ApplicationContext

class SumAction(PlotAction):
def __init__(self, plot, parent=None):

sum_icon = qt.QIcon("./matrix.png")

super(SumAction, self).__init__(
plot, icon=sum_icon, text='Sum',
tooltip='Sum 100 to each image pixel.',
triggered=self.__sum,
checkable=True, parent=parent)

def __sum(self, checked):

activeImage = self.plot.getActiveImage()

if activeImage is None:
qt.QMessageBox.information(
self.plot,
"Plot data is None. Open a valid 2D image to use this function."
)

data = activeImage.getData()

self.originalData = activeImage.getData()
data+=100
activeImage.setData(data)

class MyApplicationContext(ApplicationContext):
"""This class is shared to all the silx view application."""

def findPrintToolBar(self, plot):
# FIXME: It would be better to use the Qt API
return plot._outputToolBar

def viewWidgetCreated(self, view, widget):
"""Called when the widget of the view was created.

So we can custom it.
"""
from silx.gui.plot import Plot2D
if isinstance(widget, Plot2D):
toolBar = self.findPrintToolBar(widget)
action = SumAction(widget, widget)
toolBar.addAction(action)

class MyViewer(Viewer):
def createApplicationContext(self, settings):
return MyApplicationContext(self, settings)

window = MyViewer(parent=parent, settings=settings)
window.setWindowTitle(window.windowTitle() + " [custom]")
return window


def main(args):
from silx.app.view import main as silx_view_main
# Monkey patch the main window creation
silx_view_main.createWindow = createWindow
# Use the default launcher
silx_view_main.main(args)


if __name__ == '__main__':
main(sys.argv)
</code></pre>
 

Latest posts

ن
Replies
0
Views
1
نعمان منذر محمود الجميلي
ن
Top