【问题标题】:TypeError: setCurrentWindow(): 1st arg can't be coerced to ij.gui.ImageWindowTypeError: setCurrentWindow(): 1st arg can't be coerced to ij.gui.ImageWindow
【发布时间】:2016-09-20 17:18:41
【问题描述】:

您好,在编写脚本方面我是个新手。我正在尝试编写一个 Jython 脚本,该脚本将在 imageJ 中拍摄不在前面的图像并将其带到前面。我曾尝试使用 WindowManager,但经常遇到类似的错误。

TypeError: setCurrentWindow(): 1st arg 不能被强制为 ij.gui.ImageWindow

或其他形式的此错误。似乎激活不在前面的图像应该不会太困难。

这是我正在使用的代码:

from ij import IJ
from ij import WindowManager as WM

titles = WM.getIDList()

WM.setCurrentWindow(titles[:1]) 

【问题讨论】:

    标签: jython imagej


    【解决方案1】:

    WindowManager.setCurrentWindow 方法采用 ImageWindow 对象,而不是 int 图像 ID。但是您可以按如下方式查找给定 ID 的 ImageWindow

    WM.getImage(imageID).getWindow()
    

    这是您的代码的工作版本:

    from ij import IJ
    from ij import WindowManager as WM
    
    print("[BEFORE] Active image is: " + IJ.getImage().toString())
    
    ids = WM.getIDList()
    win = WM.getImage(ids[-1]).getWindow()
    WM.setCurrentWindow(win)
    win.toFront()
    
    print("[AFTER] Active image is: " + IJ.getImage().toString())
    

    注意事项

    • 我将您的titles 变量重命名为ids,因为WindowManager.getIDList() 方法返回int 图像ID 列表,不是String 图像标题列表。
    • WM.getImage(int imageID) 方法需要int,而不是列表。所以我使用了ids[-1],它是ids 列表的最后一个元素,而不是ids[:1],它是一个子数组。当然,您可以传递任何您想要的图像 ID。
    • 我添加了调用win.toFront(),它实际上将窗口带到了前面。调用 WM.setCurrentWindow(win) 很重要,因为它告诉 ImageJ 该图像现在是活动图像......但它实际上不会提升窗口,这正是您想要的。

    【讨论】:

    • 谢谢!我能弄明白!
    猜你喜欢
    • 2013-11-13
    • 2022-07-09
    • 2022-01-26
    • 1970-01-01
    • 2012-03-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多