question about Windowmanager.toFront()

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
8 messages Options
Reply | Threaded
Open this post in threaded view
|

question about Windowmanager.toFront()

Aryeh Weiss
I wish to select a window to be the front window in my display.  to do
this I have function called setFrontWin() that checks if the image is
displayed, shows it if needed, and then moves it to the front. I tested
setFrontWin() using the script below, that loops through all of the open
windows and moves them to the front. This works fine.

################################
def setFrontWin(imp):
     win = WindowManager.getWindow(imp.getTitle())
     if win == None:
         imp.show()
         win = WindowManager.getWindow(imp.getTitle())
     win.toFront()
     return win

for w in WindowManager.getImageTitles():
     imp = WindowManager.getImage(w)
     setFrontWin(imp)
     wd=WaitForUserDialog("wait")
     wd.show()
#################################

Below is a script where this does not work. I open an image, make a
copy, and then want the original image
to be displayed in front. However, this does not happen -- the copy is
left in front.
The test script above moves these same images as expected, and I cannot
figure out where they are different.

######################

import os
from os import sys, sep, path, makedirs

def open_image():
     """
     opens an image, returns an imagePlus object and its name in that order
     """
     # Prompt user for the input image file.
     print "open_image begin"
     op = OpenDialog("Choose input image...", "")
     print op.getFileName()

     if op.getFileName() == None:
         sys.exit('User canceled dialog')
     # open selected image and prepare it for analysis

     inputName = op.getFileName()
     inputDirPath = op.getDirectory()
     inputPath = inputDirPath + inputName

     # Strip the suffix off of the input image name
     if inputName[-4] == ".":
         inputPrefix = inputName[:-4]    # assumes that a suffix exists
     else:
         inputPrefix = inputName

     #opens image and returns it.
     inputImp = ImagePlus(inputPath)

     print "open_image finis"
     return inputImp, inputPrefix, inputDirPath

def setFrontWin(imp):
     win = WindowManager.getWindow(imp.getTitle())
     if win == None:
         imp.show()
         win = WindowManager.getWindow(imp.getTitle())
     print win
     win.toFront()
     return win

fullInputImp, inputPrefix, inputDirPath = open_image()
fullInputImp.show()
print fullInputImp, inputPrefix, inputDirPath

inputImp = fullInputImp.duplicate()
inputImp.show()
inputImp.setTitle(inputPrefix+"_copy")
fullInputImp.setTitle(inputPrefix+"_orig")
setFrontWin(fullInputImp)

#############################

For testing purposes I am using auto-import, and these scripts should be
able to run anywhere.

Where have I gone wrong?

Tnx in advance
--aryeh

--
Aryeh Weiss
Faculty of Engineering
Bar Ilan University
Ramat Gan 52900 Israel

Ph:  972-3-5317638
FAX: 972-3-7384051


--
ImageJ mailing list: http://imagej.nih.gov/ij/list.html
Reply | Threaded
Open this post in threaded view
|

Re: question about Windowmanager.toFront()

Michael Schmid
Hi Aryeh,

a safer way to put an image to the foreground is
   IJ.selectWindow(imp.getID())

Otherwise you may need to introduce time delays. Displaying a window and
bringing it to the foreground are asynchronous operations, which may be
slower than the execution of your code.

IJ.selectWindow has a loop with time delay to wait until the window is
really in the foreground.

Michael
________________________________________________________________
On 21.08.19 10:13, Aryeh Weiss wrote:
 > I wish to select a window to be the front window in my display.  to
do this I have function called setFrontWin() that checks if the image is
displayed, shows it if needed, and then moves it to the front. I tested
setFrontWin() using the script below, that loops through all of the open
windows and moves them to the front. This works fine.
 >
 > ################################
 > def setFrontWin(imp):
 >      win = WindowManager.getWindow(imp.getTitle())
 >      if win == None:
 >          imp.show()
 >          win = WindowManager.getWindow(imp.getTitle())
 >      win.toFront()
 >      return win
 >
 > for w in WindowManager.getImageTitles():
 >      imp = WindowManager.getImage(w)
 >      setFrontWin(imp)
 >      wd=WaitForUserDialog("wait")
 >      wd.show()
 > #################################
 >
 > Below is a script where this does not work. I open an image, make a
copy, and then want the original image
 > to be displayed in front. However, this does not happen -- the copy
is left in front.
 > The test script above moves these same images as expected, and I
cannot figure out where they are different.
 >
 > ######################
 >
 > import os
 > from os import sys, sep, path, makedirs
 >
 > def open_image():
 >      """
 >      opens an image, returns an imagePlus object and its name in that
order
 >      """
 >      # Prompt user for the input image file.
 >      print "open_image begin"
 >      op = OpenDialog("Choose input image...", "")
 >      print op.getFileName()
 >
 >      if op.getFileName() == None:
 >          sys.exit('User canceled dialog')
 >      # open selected image and prepare it for analysis
 >
 >      inputName = op.getFileName()
 >      inputDirPath = op.getDirectory()
 >      inputPath = inputDirPath + inputName
 >
 >      # Strip the suffix off of the input image name
 >      if inputName[-4] == ".":
 >          inputPrefix = inputName[:-4]    # assumes that a suffix exists
 >      else:
 >          inputPrefix = inputName
 >
 >      #opens image and returns it.
 >      inputImp = ImagePlus(inputPath)
 >
 >      print "open_image finis"
 >      return inputImp, inputPrefix, inputDirPath
 >
 > def setFrontWin(imp):
 >      win = WindowManager.getWindow(imp.getTitle())
 >      if win == None:
 >          imp.show()
 >          win = WindowManager.getWindow(imp.getTitle())
 >      print win
 >      win.toFront()
 >      return win
 >
 > fullInputImp, inputPrefix, inputDirPath = open_image()
 > fullInputImp.show()
 > print fullInputImp, inputPrefix, inputDirPath
 >
 > inputImp = fullInputImp.duplicate()
 > inputImp.show()
 > inputImp.setTitle(inputPrefix+"_copy")
 > fullInputImp.setTitle(inputPrefix+"_orig")
 > setFrontWin(fullInputImp)
 >
 > #############################
 >
 > For testing purposes I am using auto-import, and these scripts should
be able to run anywhere.
 >
 > Where have I gone wrong?
 >
 > Tnx in advance
 > --aryeh
 >

--
ImageJ mailing list: http://imagej.nih.gov/ij/list.html
Reply | Threaded
Open this post in threaded view
|

Re: question about Windowmanager.toFront()

Aryeh Weiss
Hi Michael,

Thank you for your quick reply. I replaced the WindowManager.toFront()
call with the IJ.selectWindow() call,.

As before, the simple test script that loops through the windows works,
while the second test script that duplicates the input window does not.

Best regards
--aryeh


On 21/08/2019 11:36, Michael Schmid wrote:

> Hi Aryeh,
>
> a safer way to put an image to the foreground is
>   IJ.selectWindow(imp.getID())
>
> Otherwise you may need to introduce time delays. Displaying a window
> and bringing it to the foreground are asynchronous operations, which
> may be slower than the execution of your code.
>
> IJ.selectWindow has a loop with time delay to wait until the window is
> really in the foreground.
>
> Michael
> ________________________________________________________________
> On 21.08.19 10:13, Aryeh Weiss wrote:
> > I wish to select a window to be the front window in my display.  to
> do this I have function called setFrontWin() that checks if the image
> is displayed, shows it if needed, and then moves it to the front. I
> tested setFrontWin() using the script below, that loops through all of
> the open windows and moves them to the front. This works fine.
> >
> > ################################
> > def setFrontWin(imp):
> >      win = WindowManager.getWindow(imp.getTitle())
> >      if win == None:
> >          imp.show()
> >          win = WindowManager.getWindow(imp.getTitle())
> >      win.toFront()
> >      return win
> >
> > for w in WindowManager.getImageTitles():
> >      imp = WindowManager.getImage(w)
> >      setFrontWin(imp)
> >      wd=WaitForUserDialog("wait")
> >      wd.show()
> > #################################
> >
> > Below is a script where this does not work. I open an image, make a
> copy, and then want the original image
> > to be displayed in front. However, this does not happen -- the copy
> is left in front.
> > The test script above moves these same images as expected, and I
> cannot figure out where they are different.
> >
> > ######################
> >
> > import os
> > from os import sys, sep, path, makedirs
> >
> > def open_image():
> >      """
> >      opens an image, returns an imagePlus object and its name in
> that order
> >      """
> >      # Prompt user for the input image file.
> >      print "open_image begin"
> >      op = OpenDialog("Choose input image...", "")
> >      print op.getFileName()
> >
> >      if op.getFileName() == None:
> >          sys.exit('User canceled dialog')
> >      # open selected image and prepare it for analysis
> >
> >      inputName = op.getFileName()
> >      inputDirPath = op.getDirectory()
> >      inputPath = inputDirPath + inputName
> >
> >      # Strip the suffix off of the input image name
> >      if inputName[-4] == ".":
> >          inputPrefix = inputName[:-4]    # assumes that a suffix exists
> >      else:
> >          inputPrefix = inputName
> >
> >      #opens image and returns it.
> >      inputImp = ImagePlus(inputPath)
> >
> >      print "open_image finis"
> >      return inputImp, inputPrefix, inputDirPath
> >
> > def setFrontWin(imp):
> >      win = WindowManager.getWindow(imp.getTitle())
> >      if win == None:
> >          imp.show()
> >          win = WindowManager.getWindow(imp.getTitle())
> >      print win
> >      win.toFront()
> >      return win
> >
> > fullInputImp, inputPrefix, inputDirPath = open_image()
> > fullInputImp.show()
> > print fullInputImp, inputPrefix, inputDirPath
> >
> > inputImp = fullInputImp.duplicate()
> > inputImp.show()
> > inputImp.setTitle(inputPrefix+"_copy")
> > fullInputImp.setTitle(inputPrefix+"_orig")
> > setFrontWin(fullInputImp)
> >
> > #############################
> >
> > For testing purposes I am using auto-import, and these scripts
> should be able to run anywhere.
> >
> > Where have I gone wrong?
> >
> > Tnx in advance
> > --aryeh
> >
>
> --
> ImageJ mailing list: http://imagej.nih.gov/ij/list.html
>

--
Aryeh Weiss
Faculty of Engineering
Bar Ilan University
Ramat Gan 52900 Israel

Ph:  972-3-5317638
FAX: 972-3-7384051

--
ImageJ mailing list: http://imagej.nih.gov/ij/list.html
Reply | Threaded
Open this post in threaded view
|

Re: question about Windowmanager.toFront()

Kenneth Sloan-2
I'm not a script hacker, but...

You have two definitions of setFrontWin.  One has a "print  win" in it - the other does not.

"of course" this is irrelevant, but my first step would be to make the two definitions of setFrontWind
exactly identical.

Even better, I would combine the two tests into one script, using only ONE definition of setFrontWin.

Perhaps irrelevant, but I hope this is helpful.

--
Kenneth Sloan
[hidden email]
Vision is the art of seeing what is invisible to others.





> On Aug 21, 2019, at 03:49, Aryeh Weiss <[hidden email]> wrote:
>
> Hi Michael,
>
> Thank you for your quick reply. I replaced the WindowManager.toFront() call with the IJ.selectWindow() call,.
>
> As before, the simple test script that loops through the windows works, while the second test script that duplicates the input window does not.
>
> Best regards
> --aryeh
>
>
> On 21/08/2019 11:36, Michael Schmid wrote:
>> Hi Aryeh,
>>
>> a safer way to put an image to the foreground is
>>   IJ.selectWindow(imp.getID())
>>
>> Otherwise you may need to introduce time delays. Displaying a window and bringing it to the foreground are asynchronous operations, which may be slower than the execution of your code.
>>
>> IJ.selectWindow has a loop with time delay to wait until the window is really in the foreground.
>>
>> Michael
>> ________________________________________________________________
>> On 21.08.19 10:13, Aryeh Weiss wrote:
>> > I wish to select a window to be the front window in my display.  to do this I have function called setFrontWin() that checks if the image is displayed, shows it if needed, and then moves it to the front. I tested setFrontWin() using the script below, that loops through all of the open windows and moves them to the front. This works fine.
>> >
>> > ################################
>> > def setFrontWin(imp):
>> >      win = WindowManager.getWindow(imp.getTitle())
>> >      if win == None:
>> >          imp.show()
>> >          win = WindowManager.getWindow(imp.getTitle())
>> >      win.toFront()
>> >      return win
>> >
>> > for w in WindowManager.getImageTitles():
>> >      imp = WindowManager.getImage(w)
>> >      setFrontWin(imp)
>> >      wd=WaitForUserDialog("wait")
>> >      wd.show()
>> > #################################
>> >
>> > Below is a script where this does not work. I open an image, make a copy, and then want the original image
>> > to be displayed in front. However, this does not happen -- the copy is left in front.
>> > The test script above moves these same images as expected, and I cannot figure out where they are different.
>> >
>> > ######################
>> >
>> > import os
>> > from os import sys, sep, path, makedirs
>> >
>> > def open_image():
>> >      """
>> >      opens an image, returns an imagePlus object and its name in that order
>> >      """
>> >      # Prompt user for the input image file.
>> >      print "open_image begin"
>> >      op = OpenDialog("Choose input image...", "")
>> >      print op.getFileName()
>> >
>> >      if op.getFileName() == None:
>> >          sys.exit('User canceled dialog')
>> >      # open selected image and prepare it for analysis
>> >
>> >      inputName = op.getFileName()
>> >      inputDirPath = op.getDirectory()
>> >      inputPath = inputDirPath + inputName
>> >
>> >      # Strip the suffix off of the input image name
>> >      if inputName[-4] == ".":
>> >          inputPrefix = inputName[:-4]    # assumes that a suffix exists
>> >      else:
>> >          inputPrefix = inputName
>> >
>> >      #opens image and returns it.
>> >      inputImp = ImagePlus(inputPath)
>> >
>> >      print "open_image finis"
>> >      return inputImp, inputPrefix, inputDirPath
>> >
>> > def setFrontWin(imp):
>> >      win = WindowManager.getWindow(imp.getTitle())
>> >      if win == None:
>> >          imp.show()
>> >          win = WindowManager.getWindow(imp.getTitle())
>> >      print win
>> >      win.toFront()
>> >      return win
>> >
>> > fullInputImp, inputPrefix, inputDirPath = open_image()
>> > fullInputImp.show()
>> > print fullInputImp, inputPrefix, inputDirPath
>> >
>> > inputImp = fullInputImp.duplicate()
>> > inputImp.show()
>> > inputImp.setTitle(inputPrefix+"_copy")
>> > fullInputImp.setTitle(inputPrefix+"_orig")
>> > setFrontWin(fullInputImp)
>> >
>> > #############################
>> >
>> > For testing purposes I am using auto-import, and these scripts should be able to run anywhere.
>> >
>> > Where have I gone wrong?
>> >
>> > Tnx in advance
>> > --aryeh
>> >
>>
>> --
>> ImageJ mailing list: http://imagej.nih.gov/ij/list.html
>>
>
> --
> Aryeh Weiss
> Faculty of Engineering
> Bar Ilan University
> Ramat Gan 52900 Israel
>
> Ph:  972-3-5317638
> FAX: 972-3-7384051
>
> --
> ImageJ mailing list: http://imagej.nih.gov/ij/list.html

--
ImageJ mailing list: http://imagej.nih.gov/ij/list.html
Reply | Threaded
Open this post in threaded view
|

Re: question about Windowmanager.toFront()

Michael Schmid
In reply to this post by Aryeh Weiss
Hi Aryeh,

it is clearly a timing issue.

The following script works *sometimes* (using the "Evaluate Python" of
the ImageJ Editor). If it works correctly, it takes the '._orig' to the
foreground, and also the Window menu of the ImageJ panel shows it
checked as foreground image.
In a few cases, the "_orig" goes to the foreground, and
WindowManager.getCurrentImage() reports it as foreground image at that
moment, but 100 ms later, the second IJ.getImage() command shows the
'_copy' as a front image.
In that case, also the Window menu of the ImageJ main panel shows the
"_copy" as foreground, and any successive operation such as "Gaussian
Blur" also affects the "_copy".

I guess that it takes some time to display the input image after opening
it (which is an asynchronous operation), and when it gets displayed, it
may become the foreground image.

Adding a 'Thread.sleep(100)' after showing the input image seems to fix
the problem.


In macros, this does not happen because ImagePlus.show() checks for
IJ.macrorunning, which is probably not set by running a Python script.
This would mean that the problem can be also solved by using
IJ.open(inputPath) or IJ.openImage(inputPath) instead of
ImagePlus(inputPath).
IJ.open/openImage sets IJ.macrorunning, so it should ensure that the
image gets displayed before continuing.

---
By the way, I had to comment out the import statements to make it run.

ImageJ 1.52q40; Java 1.8.0_112 [64-bit]; Linux 4.4.0-159-generic; 46MB
of 10000MB (<1%)
ImageJ/plugins/Jython.jar is from Sept 2017

________________________________________________________________
#import os
#from os import sys, sep, path, makedirs

def open_image():
     """
     opens an image, returns an imagePlus object and its name in that order
     """
     # Prompt user for the input image file.
     print "open_image begin"
     op = OpenDialog("Choose input image...", "")
     print op.getFileName()

     if op.getFileName() == None:
         sys.exit('User canceled dialog')
     # open selected image and prepare it for analysis

     inputName = op.getFileName()
     inputDirPath = op.getDirectory()
     inputPath = inputDirPath + inputName

     # Strip the suffix off of the input image name
     if inputName[-4] == ".":
         inputPrefix = inputName[:-4]    # assumes that a suffix exists
     else:
         inputPrefix = inputName

     #opens image and returns it.
     inputImp = ImagePlus(inputPath)

     print "open_image finis"
     return inputImp, inputPrefix, inputDirPath

def setFrontWin(imp):
     win = imp.getWindow()
     if win == None:
         imp.show()
         win =  imp.getWindow()
     print "toFront: ", win
     IJ.selectWindow(imp.getID())
     print "selected: ", WindowManager.getCurrentImage()
     print "getImage: ", IJ.getImage()
     Thread.sleep(100)
     print "getImage: ", IJ.getImage()
     return win

fullInputImp, inputPrefix, inputDirPath = open_image()
fullInputImp.show()
print fullInputImp, inputPrefix, inputDirPath

inputImp = fullInputImp.duplicate()
inputImp.show()
inputImp.setTitle(inputPrefix+"_copy")
fullInputImp.setTitle(inputPrefix+"_orig")
setFrontWin(fullInputImp)
________________________________________________________________

Best,

Michael
________________________________________________________________
On 21.08.19 10:49, Aryeh Weiss wrote:

> Hi Michael,
>
> Thank you for your quick reply. I replaced the WindowManager.toFront()
> call with the IJ.selectWindow() call,.
>
> As before, the simple test script that loops through the windows works,
> while the second test script that duplicates the input window does not.
>
> Best regards
> --aryeh
>
>
> On 21/08/2019 11:36, Michael Schmid wrote:
>> Hi Aryeh,
>>
>> a safer way to put an image to the foreground is
>>   IJ.selectWindow(imp.getID())
>>
>> Otherwise you may need to introduce time delays. Displaying a window
>> and bringing it to the foreground are asynchronous operations, which
>> may be slower than the execution of your code.
>>
>> IJ.selectWindow has a loop with time delay to wait until the window is
>> really in the foreground.
>>
>> Michael
>> ________________________________________________________________
>> On 21.08.19 10:13, Aryeh Weiss wrote:
>> > I wish to select a window to be the front window in my display.  to
>> do this I have function called setFrontWin() that checks if the image
>> is displayed, shows it if needed, and then moves it to the front. I
>> tested setFrontWin() using the script below, that loops through all of
>> the open windows and moves them to the front. This works fine.
>> >
>> > ################################
>> > def setFrontWin(imp):
>> >      win = WindowManager.getWindow(imp.getTitle())
>> >      if win == None:
>> >          imp.show()
>> >          win = WindowManager.getWindow(imp.getTitle())
>> >      win.toFront()
>> >      return win
>> >
>> > for w in WindowManager.getImageTitles():
>> >      imp = WindowManager.getImage(w)
>> >      setFrontWin(imp)
>> >      wd=WaitForUserDialog("wait")
>> >      wd.show()
>> > #################################
>> >
>> > Below is a script where this does not work. I open an image, make a
>> copy, and then want the original image
>> > to be displayed in front. However, this does not happen -- the copy
>> is left in front.
>> > The test script above moves these same images as expected, and I
>> cannot figure out where they are different.
>> >
>> > ######################
>> >
>> > import os
>> > from os import sys, sep, path, makedirs
>> >
>> > def open_image():
>> >      """
>> >      opens an image, returns an imagePlus object and its name in
>> that order
>> >      """
>> >      # Prompt user for the input image file.
>> >      print "open_image begin"
>> >      op = OpenDialog("Choose input image...", "")
>> >      print op.getFileName()
>> >
>> >      if op.getFileName() == None:
>> >          sys.exit('User canceled dialog')
>> >      # open selected image and prepare it for analysis
>> >
>> >      inputName = op.getFileName()
>> >      inputDirPath = op.getDirectory()
>> >      inputPath = inputDirPath + inputName
>> >
>> >      # Strip the suffix off of the input image name
>> >      if inputName[-4] == ".":
>> >          inputPrefix = inputName[:-4]    # assumes that a suffix exists
>> >      else:
>> >          inputPrefix = inputName
>> >
>> >      #opens image and returns it.
>> >      inputImp = ImagePlus(inputPath)
>> >
>> >      print "open_image finis"
>> >      return inputImp, inputPrefix, inputDirPath
>> >
>> > def setFrontWin(imp):
>> >      win = WindowManager.getWindow(imp.getTitle())
>> >      if win == None:
>> >          imp.show()
>> >          win = WindowManager.getWindow(imp.getTitle())
>> >      print win
>> >      win.toFront()
>> >      return win
>> >
>> > fullInputImp, inputPrefix, inputDirPath = open_image()
>> > fullInputImp.show()
>> > print fullInputImp, inputPrefix, inputDirPath
>> >
>> > inputImp = fullInputImp.duplicate()
>> > inputImp.show()
>> > inputImp.setTitle(inputPrefix+"_copy")
>> > fullInputImp.setTitle(inputPrefix+"_orig")
>> > setFrontWin(fullInputImp)
>> >
>> > #############################
>> >
>> > For testing purposes I am using auto-import, and these scripts
>> should be able to run anywhere.
>> >
>> > Where have I gone wrong?
>> >
>> > Tnx in advance
>> > --aryeh
>> >
>>
>> --
>> ImageJ mailing list: http://imagej.nih.gov/ij/list.html
>>
>

--
ImageJ mailing list: http://imagej.nih.gov/ij/list.html
Reply | Threaded
Open this post in threaded view
|

Re: question about Windowmanager.toFront()

Kenneth Sloan-2
A  'Thread.sleep(100)' may work NOW (on some machines), but it seems like a kludge.  Is there a better
way for a script to wait for/check for/receive a signal that an asynchronous operation has completed?

Can a script SET IJ.macrorunning explicitly?  That would also work (but then "macrorunning" is no longer quite
the right name).  

How about a generic "wait for all asynchronous operations to complete"?  I would like that better than "sleep(100) and hope that's long enough".

Is there a list, somewhere, of all asynchronous operations in IJ?

And finally...is this an issue for Java (not javascript) plugins?  That might explain some weird behavior I have seen
in the  past and managed to muddle through without really understanding them.  That is - what would you predict would be the behavior if the script were transliterated into Java and run (with, and without, the Thread.sleep)?

--
Kenneth Sloan
[hidden email]
Vision is the art of seeing what is invisible to others.





> On Aug 21, 2019, at 10:00, Michael Schmid <[hidden email]> wrote:
>
> Hi Aryeh,
>
> it is clearly a timing issue.
> The following script works *sometimes* (using the "Evaluate Python" of the ImageJ Editor). If it works correctly, it takes the '._orig' to the foreground, and also the Window menu of the ImageJ panel shows it checked as foreground image.
> In a few cases, the "_orig" goes to the foreground, and WindowManager.getCurrentImage() reports it as foreground image at that moment, but 100 ms later, the second IJ.getImage() command shows the '_copy' as a front image.
> In that case, also the Window menu of the ImageJ main panel shows the "_copy" as foreground, and any successive operation such as "Gaussian Blur" also affects the "_copy".
>
> I guess that it takes some time to display the input image after opening it (which is an asynchronous operation), and when it gets displayed, it may become the foreground image.
>
> Adding a 'Thread.sleep(100)' after showing the input image seems to fix the problem.
>
>
> In macros, this does not happen because ImagePlus.show() checks for IJ.macrorunning, which is probably not set by running a Python script.
> This would mean that the problem can be also solved by using IJ.open(inputPath) or IJ.openImage(inputPath) instead of ImagePlus(inputPath).
> IJ.open/openImage sets IJ.macrorunning, so it should ensure that the image gets displayed before continuing.
>
> ---
> By the way, I had to comment out the import statements to make it run.
>
> ImageJ 1.52q40; Java 1.8.0_112 [64-bit]; Linux 4.4.0-159-generic; 46MB of 10000MB (<1%)
> ImageJ/plugins/Jython.jar is from Sept 2017
>
> ________________________________________________________________
> #import os
> #from os import sys, sep, path, makedirs
>
> def open_image():
>    """
>    opens an image, returns an imagePlus object and its name in that order
>    """
>    # Prompt user for the input image file.
>    print "open_image begin"
>    op = OpenDialog("Choose input image...", "")
>    print op.getFileName()
>
>    if op.getFileName() == None:
>        sys.exit('User canceled dialog')
>    # open selected image and prepare it for analysis
>
>    inputName = op.getFileName()
>    inputDirPath = op.getDirectory()
>    inputPath = inputDirPath + inputName
>
>    # Strip the suffix off of the input image name
>    if inputName[-4] == ".":
>        inputPrefix = inputName[:-4]    # assumes that a suffix exists
>    else:
>        inputPrefix = inputName
>
>    #opens image and returns it.
>    inputImp = ImagePlus(inputPath)
>
>    print "open_image finis"
>    return inputImp, inputPrefix, inputDirPath
>
> def setFrontWin(imp):
>    win = imp.getWindow()
>    if win == None:
>        imp.show()
>        win =  imp.getWindow()
>    print "toFront: ", win
>    IJ.selectWindow(imp.getID())
>    print "selected: ", WindowManager.getCurrentImage()
>    print "getImage: ", IJ.getImage()
>    Thread.sleep(100)
>    print "getImage: ", IJ.getImage()
>    return win
>
> fullInputImp, inputPrefix, inputDirPath = open_image()
> fullInputImp.show()
> print fullInputImp, inputPrefix, inputDirPath
>
> inputImp = fullInputImp.duplicate()
> inputImp.show()
> inputImp.setTitle(inputPrefix+"_copy")
> fullInputImp.setTitle(inputPrefix+"_orig")
> setFrontWin(fullInputImp) ________________________________________________________________
>
> Best,
>
> Michael
> ________________________________________________________________
> On 21.08.19 10:49, Aryeh Weiss wrote:
>> Hi Michael,
>> Thank you for your quick reply. I replaced the WindowManager.toFront() call with the IJ.selectWindow() call,.
>> As before, the simple test script that loops through the windows works, while the second test script that duplicates the input window does not.
>> Best regards
>> --aryeh
>> On 21/08/2019 11:36, Michael Schmid wrote:
>>> Hi Aryeh,
>>>
>>> a safer way to put an image to the foreground is
>>>   IJ.selectWindow(imp.getID())
>>>
>>> Otherwise you may need to introduce time delays. Displaying a window and bringing it to the foreground are asynchronous operations, which may be slower than the execution of your code.
>>>
>>> IJ.selectWindow has a loop with time delay to wait until the window is really in the foreground.
>>>
>>> Michael
>>> ________________________________________________________________
>>> On 21.08.19 10:13, Aryeh Weiss wrote:
>>> > I wish to select a window to be the front window in my display.  to do this I have function called setFrontWin() that checks if the image is displayed, shows it if needed, and then moves it to the front. I tested setFrontWin() using the script below, that loops through all of the open windows and moves them to the front. This works fine.
>>> >
>>> > ################################
>>> > def setFrontWin(imp):
>>> >      win = WindowManager.getWindow(imp.getTitle())
>>> >      if win == None:
>>> >          imp.show()
>>> >          win = WindowManager.getWindow(imp.getTitle())
>>> >      win.toFront()
>>> >      return win
>>> >
>>> > for w in WindowManager.getImageTitles():
>>> >      imp = WindowManager.getImage(w)
>>> >      setFrontWin(imp)
>>> >      wd=WaitForUserDialog("wait")
>>> >      wd.show()
>>> > #################################
>>> >
>>> > Below is a script where this does not work. I open an image, make a copy, and then want the original image
>>> > to be displayed in front. However, this does not happen -- the copy is left in front.
>>> > The test script above moves these same images as expected, and I cannot figure out where they are different.
>>> >
>>> > ######################
>>> >
>>> > import os
>>> > from os import sys, sep, path, makedirs
>>> >
>>> > def open_image():
>>> >      """
>>> >      opens an image, returns an imagePlus object and its name in that order
>>> >      """
>>> >      # Prompt user for the input image file.
>>> >      print "open_image begin"
>>> >      op = OpenDialog("Choose input image...", "")
>>> >      print op.getFileName()
>>> >
>>> >      if op.getFileName() == None:
>>> >          sys.exit('User canceled dialog')
>>> >      # open selected image and prepare it for analysis
>>> >
>>> >      inputName = op.getFileName()
>>> >      inputDirPath = op.getDirectory()
>>> >      inputPath = inputDirPath + inputName
>>> >
>>> >      # Strip the suffix off of the input image name
>>> >      if inputName[-4] == ".":
>>> >          inputPrefix = inputName[:-4]    # assumes that a suffix exists
>>> >      else:
>>> >          inputPrefix = inputName
>>> >
>>> >      #opens image and returns it.
>>> >      inputImp = ImagePlus(inputPath)
>>> >
>>> >      print "open_image finis"
>>> >      return inputImp, inputPrefix, inputDirPath
>>> >
>>> > def setFrontWin(imp):
>>> >      win = WindowManager.getWindow(imp.getTitle())
>>> >      if win == None:
>>> >          imp.show()
>>> >          win = WindowManager.getWindow(imp.getTitle())
>>> >      print win
>>> >      win.toFront()
>>> >      return win
>>> >
>>> > fullInputImp, inputPrefix, inputDirPath = open_image()
>>> > fullInputImp.show()
>>> > print fullInputImp, inputPrefix, inputDirPath
>>> >
>>> > inputImp = fullInputImp.duplicate()
>>> > inputImp.show()
>>> > inputImp.setTitle(inputPrefix+"_copy")
>>> > fullInputImp.setTitle(inputPrefix+"_orig")
>>> > setFrontWin(fullInputImp)
>>> >
>>> > #############################
>>> >
>>> > For testing purposes I am using auto-import, and these scripts should be able to run anywhere.
>>> >
>>> > Where have I gone wrong?
>>> >
>>> > Tnx in advance
>>> > --aryeh
>>> >
>>>
>>> --
>>> ImageJ mailing list: http://imagej.nih.gov/ij/list.html
>>>
>
> --
> ImageJ mailing list: http://imagej.nih.gov/ij/list.html

--
ImageJ mailing list: http://imagej.nih.gov/ij/list.html
Reply | Threaded
Open this post in threaded view
|

Re: question about Windowmanager.toFront()

Aryeh Weiss
In reply to this post by Kenneth Sloan-2
Thanks for your reply. I tested identical scripts. That print statement
was added after they produced different results just to make sure that
the expected image window was being used as the argument.

--aryeh

On 21/08/2019 18:00, Kenneth Sloan wrote:

> I'm not a script hacker, but...
>
> You have two definitions of setFrontWin.  One has a "print  win" in it - the other does not.
>
> "of course" this is irrelevant, but my first step would be to make the two definitions of setFrontWind
> exactly identical.
>
> Even better, I would combine the two tests into one script, using only ONE definition of setFrontWin.
>
> Perhaps irrelevant, but I hope this is helpful.
>
> --
> Kenneth Sloan
> [hidden email]
> Vision is the art of seeing what is invisible to others.
>
>
>
>
>
>> On Aug 21, 2019, at 03:49, Aryeh Weiss <[hidden email]> wrote:
>>
>> Hi Michael,
>>
>> Thank you for your quick reply. I replaced the WindowManager.toFront() call with the IJ.selectWindow() call,.
>>
>> As before, the simple test script that loops through the windows works, while the second test script that duplicates the input window does not.
>>
>> Best regards
>> --aryeh
>>
>>
>> On 21/08/2019 11:36, Michael Schmid wrote:
>>> Hi Aryeh,
>>>
>>> a safer way to put an image to the foreground is
>>>    IJ.selectWindow(imp.getID())
>>>
>>> Otherwise you may need to introduce time delays. Displaying a window and bringing it to the foreground are asynchronous operations, which may be slower than the execution of your code.
>>>
>>> IJ.selectWindow has a loop with time delay to wait until the window is really in the foreground.
>>>
>>> Michael
>>> ________________________________________________________________
>>> On 21.08.19 10:13, Aryeh Weiss wrote:
>>>> I wish to select a window to be the front window in my display.  to do this I have function called setFrontWin() that checks if the image is displayed, shows it if needed, and then moves it to the front. I tested setFrontWin() using the script below, that loops through all of the open windows and moves them to the front. This works fine.
>>>>
>>>> ################################
>>>> def setFrontWin(imp):
>>>>       win = WindowManager.getWindow(imp.getTitle())
>>>>       if win == None:
>>>>           imp.show()
>>>>           win = WindowManager.getWindow(imp.getTitle())
>>>>       win.toFront()
>>>>       return win
>>>>
>>>> for w in WindowManager.getImageTitles():
>>>>       imp = WindowManager.getImage(w)
>>>>       setFrontWin(imp)
>>>>       wd=WaitForUserDialog("wait")
>>>>       wd.show()
>>>> #################################
>>>>
>>>> Below is a script where this does not work. I open an image, make a copy, and then want the original image
>>>> to be displayed in front. However, this does not happen -- the copy is left in front.
>>>> The test script above moves these same images as expected, and I cannot figure out where they are different.
>>>>
>>>> ######################
>>>>
>>>> import os
>>>> from os import sys, sep, path, makedirs
>>>>
>>>> def open_image():
>>>>       """
>>>>       opens an image, returns an imagePlus object and its name in that order
>>>>       """
>>>>       # Prompt user for the input image file.
>>>>       print "open_image begin"
>>>>       op = OpenDialog("Choose input image...", "")
>>>>       print op.getFileName()
>>>>
>>>>       if op.getFileName() == None:
>>>>           sys.exit('User canceled dialog')
>>>>       # open selected image and prepare it for analysis
>>>>
>>>>       inputName = op.getFileName()
>>>>       inputDirPath = op.getDirectory()
>>>>       inputPath = inputDirPath + inputName
>>>>
>>>>       # Strip the suffix off of the input image name
>>>>       if inputName[-4] == ".":
>>>>           inputPrefix = inputName[:-4]    # assumes that a suffix exists
>>>>       else:
>>>>           inputPrefix = inputName
>>>>
>>>>       #opens image and returns it.
>>>>       inputImp = ImagePlus(inputPath)
>>>>
>>>>       print "open_image finis"
>>>>       return inputImp, inputPrefix, inputDirPath
>>>>
>>>> def setFrontWin(imp):
>>>>       win = WindowManager.getWindow(imp.getTitle())
>>>>       if win == None:
>>>>           imp.show()
>>>>           win = WindowManager.getWindow(imp.getTitle())
>>>>       print win
>>>>       win.toFront()
>>>>       return win
>>>>
>>>> fullInputImp, inputPrefix, inputDirPath = open_image()
>>>> fullInputImp.show()
>>>> print fullInputImp, inputPrefix, inputDirPath
>>>>
>>>> inputImp = fullInputImp.duplicate()
>>>> inputImp.show()
>>>> inputImp.setTitle(inputPrefix+"_copy")
>>>> fullInputImp.setTitle(inputPrefix+"_orig")
>>>> setFrontWin(fullInputImp)
>>>>
>>>> #############################
>>>>
>>>> For testing purposes I am using auto-import, and these scripts should be able to run anywhere.
>>>>
>>>> Where have I gone wrong?
>>>>
>>>> Tnx in advance
>>>> --aryeh
>>>>
>>> --
>>> ImageJ mailing list: http://imagej.nih.gov/ij/list.html
>>>
>> --
>> Aryeh Weiss
>> Faculty of Engineering
>> Bar Ilan University
>> Ramat Gan 52900 Israel
>>
>> Ph:  972-3-5317638
>> FAX: 972-3-7384051
>>
>> --
>> ImageJ mailing list: http://imagej.nih.gov/ij/list.html
> .
>

--
Aryeh Weiss
Faculty of Engineering
Bar Ilan University
Ramat Gan 52900 Israel

Ph:  972-3-5317638
FAX: 972-3-7384051

--
ImageJ mailing list: http://imagej.nih.gov/ij/list.html
Reply | Threaded
Open this post in threaded view
|

Re: question about Windowmanager.toFront()

Aryeh Weiss
In reply to this post by Michael Schmid
Hi Michael and thanks for your reply and for working on it.
I find that just by adding time.sleep(1) after inputImp.show(), it works
as expected.



fullInputImp, inputPrefix, inputDirPath = open_image()
fullInputImp.show()
print fullInputImp, inputPrefix, inputDirPath

inputImp = fullInputImp.duplicate()
inputImp.show()
time.sleep(1)  # THIS IS THE ADDED DELAY
inputImp.setTitle(inputPrefix+"_copy")
fullInputImp.setTitle(inputPrefix+"_orig")
setFrontWin(fullInputImp)

putting the delay inside the setFrontWin function did not change anything.
Since it appears to be a timing issue, I will note that my environment is:
1. Ubuntu 19.04 (POP_OS)
2. Lenovo X1 extreme with 64GB RAM
3. 4K display using NVidia drivers and graphics (did not try it with
integrated graphics).

I have had a number of times when stuff happened that appeared to
involve timing, but this is the first time that i cold actually create a
minimal script that reproduced it.
What finally got me to use the API ParticleAnalyzer was that with the
IJ.run version my image would sometimes remain locked, and that would
hang the script. I wrote a little script that looped and ran the PA
through IJ.run a bunch of times, but it just ran perfectly and laughed
at me... I put in imp.unlock right after the call to IJ.run, but that
did not help because it was apparently stuck trying to "Show All" from
the RoiManager (I use the add to manager option in my call). But I did
not report it because without a way to reproduce it there is not much
that can be done.

Best regards
--aryeh

On 21/08/2019 18:00, Michael Schmid wrote:

> Hi Aryeh,
>
> it is clearly a timing issue.
>
> The following script works *sometimes* (using the "Evaluate Python" of
> the ImageJ Editor). If it works correctly, it takes the '._orig' to
> the foreground, and also the Window menu of the ImageJ panel shows it
> checked as foreground image.
> In a few cases, the "_orig" goes to the foreground, and
> WindowManager.getCurrentImage() reports it as foreground image at that
> moment, but 100 ms later, the second IJ.getImage() command shows the
> '_copy' as a front image.
> In that case, also the Window menu of the ImageJ main panel shows the
> "_copy" as foreground, and any successive operation such as "Gaussian
> Blur" also affects the "_copy".
>
> I guess that it takes some time to display the input image after
> opening it (which is an asynchronous operation), and when it gets
> displayed, it may become the foreground image.
>
> Adding a 'Thread.sleep(100)' after showing the input image seems to
> fix the problem.
>
>
> In macros, this does not happen because ImagePlus.show() checks for
> IJ.macrorunning, which is probably not set by running a Python script.
> This would mean that the problem can be also solved by using
> IJ.open(inputPath) or IJ.openImage(inputPath) instead of
> ImagePlus(inputPath).
> IJ.open/openImage sets IJ.macrorunning, so it should ensure that the
> image gets displayed before continuing.
>
> ---
> By the way, I had to comment out the import statements to make it run.
>
> ImageJ 1.52q40; Java 1.8.0_112 [64-bit]; Linux 4.4.0-159-generic; 46MB
> of 10000MB (<1%)
> ImageJ/plugins/Jython.jar is from Sept 2017
>
> ________________________________________________________________
> #import os
> #from os import sys, sep, path, makedirs
>
> def open_image():
>     """
>     opens an image, returns an imagePlus object and its name in that
> order
>     """
>     # Prompt user for the input image file.
>     print "open_image begin"
>     op = OpenDialog("Choose input image...", "")
>     print op.getFileName()
>
>     if op.getFileName() == None:
>         sys.exit('User canceled dialog')
>     # open selected image and prepare it for analysis
>
>     inputName = op.getFileName()
>     inputDirPath = op.getDirectory()
>     inputPath = inputDirPath + inputName
>
>     # Strip the suffix off of the input image name
>     if inputName[-4] == ".":
>         inputPrefix = inputName[:-4]    # assumes that a suffix exists
>     else:
>         inputPrefix = inputName
>
>     #opens image and returns it.
>     inputImp = ImagePlus(inputPath)
>
>     print "open_image finis"
>     return inputImp, inputPrefix, inputDirPath
>
> def setFrontWin(imp):
>     win = imp.getWindow()
>     if win == None:
>         imp.show()
>         win =  imp.getWindow()
>     print "toFront: ", win
>     IJ.selectWindow(imp.getID())
>     print "selected: ", WindowManager.getCurrentImage()
>     print "getImage: ", IJ.getImage()
>     Thread.sleep(100)
>     print "getImage: ", IJ.getImage()
>     return win
>
> fullInputImp, inputPrefix, inputDirPath = open_image()
> fullInputImp.show()
> print fullInputImp, inputPrefix, inputDirPath
>
> inputImp = fullInputImp.duplicate()
> inputImp.show()
> inputImp.setTitle(inputPrefix+"_copy")
> fullInputImp.setTitle(inputPrefix+"_orig")
> setFrontWin(fullInputImp)
> ________________________________________________________________
>
> Best,
>
> Michael
> ________________________________________________________________
> On 21.08.19 10:49, Aryeh Weiss wrote:
>> Hi Michael,
>>
>> Thank you for your quick reply. I replaced the
>> WindowManager.toFront() call with the IJ.selectWindow() call,.
>>
>> As before, the simple test script that loops through the windows
>> works, while the second test script that duplicates the input window
>> does not.
>>
>> Best regards
>> --aryeh
>>
>>
>> On 21/08/2019 11:36, Michael Schmid wrote:
>>> Hi Aryeh,
>>>
>>> a safer way to put an image to the foreground is
>>>   IJ.selectWindow(imp.getID())
>>>
>>> Otherwise you may need to introduce time delays. Displaying a window
>>> and bringing it to the foreground are asynchronous operations, which
>>> may be slower than the execution of your code.
>>>
>>> IJ.selectWindow has a loop with time delay to wait until the window
>>> is really in the foreground.
>>>
>>> Michael
>>> ________________________________________________________________
>>> On 21.08.19 10:13, Aryeh Weiss wrote:
>>> > I wish to select a window to be the front window in my display. 
>>> to do this I have function called setFrontWin() that checks if the
>>> image is displayed, shows it if needed, and then moves it to the
>>> front. I tested setFrontWin() using the script below, that loops
>>> through all of the open windows and moves them to the front. This
>>> works fine.
>>> >
>>> > ################################
>>> > def setFrontWin(imp):
>>> >      win = WindowManager.getWindow(imp.getTitle())
>>> >      if win == None:
>>> >          imp.show()
>>> >          win = WindowManager.getWindow(imp.getTitle())
>>> >      win.toFront()
>>> >      return win
>>> >
>>> > for w in WindowManager.getImageTitles():
>>> >      imp = WindowManager.getImage(w)
>>> >      setFrontWin(imp)
>>> >      wd=WaitForUserDialog("wait")
>>> >      wd.show()
>>> > #################################
>>> >
>>> > Below is a script where this does not work. I open an image, make
>>> a copy, and then want the original image
>>> > to be displayed in front. However, this does not happen -- the
>>> copy is left in front.
>>> > The test script above moves these same images as expected, and I
>>> cannot figure out where they are different.
>>> >
>>> > ######################
>>> >
>>> > import os
>>> > from os import sys, sep, path, makedirs
>>> >
>>> > def open_image():
>>> >      """
>>> >      opens an image, returns an imagePlus object and its name in
>>> that order
>>> >      """
>>> >      # Prompt user for the input image file.
>>> >      print "open_image begin"
>>> >      op = OpenDialog("Choose input image...", "")
>>> >      print op.getFileName()
>>> >
>>> >      if op.getFileName() == None:
>>> >          sys.exit('User canceled dialog')
>>> >      # open selected image and prepare it for analysis
>>> >
>>> >      inputName = op.getFileName()
>>> >      inputDirPath = op.getDirectory()
>>> >      inputPath = inputDirPath + inputName
>>> >
>>> >      # Strip the suffix off of the input image name
>>> >      if inputName[-4] == ".":
>>> >          inputPrefix = inputName[:-4]    # assumes that a suffix
>>> exists
>>> >      else:
>>> >          inputPrefix = inputName
>>> >
>>> >      #opens image and returns it.
>>> >      inputImp = ImagePlus(inputPath)
>>> >
>>> >      print "open_image finis"
>>> >      return inputImp, inputPrefix, inputDirPath
>>> >
>>> > def setFrontWin(imp):
>>> >      win = WindowManager.getWindow(imp.getTitle())
>>> >      if win == None:
>>> >          imp.show()
>>> >          win = WindowManager.getWindow(imp.getTitle())
>>> >      print win
>>> >      win.toFront()
>>> >      return win
>>> >
>>> > fullInputImp, inputPrefix, inputDirPath = open_image()
>>> > fullInputImp.show()
>>> > print fullInputImp, inputPrefix, inputDirPath
>>> >
>>> > inputImp = fullInputImp.duplicate()
>>> > inputImp.show()
>>> > inputImp.setTitle(inputPrefix+"_copy")
>>> > fullInputImp.setTitle(inputPrefix+"_orig")
>>> > setFrontWin(fullInputImp)
>>> >
>>> > #############################
>>> >
>>> > For testing purposes I am using auto-import, and these scripts
>>> should be able to run anywhere.
>>> >
>>> > Where have I gone wrong?
>>> >
>>> > Tnx in advance
>>> > --aryeh
>>> >
>>>
>>> --
>>> ImageJ mailing list: http://imagej.nih.gov/ij/list.html
>>>
>>
> .
>

--
Aryeh Weiss
Faculty of Engineering
Bar Ilan University
Ramat Gan 52900 Israel

Ph:  972-3-5317638
FAX: 972-3-7384051

--
ImageJ mailing list: http://imagej.nih.gov/ij/list.html