当调用的图像在 ImageJ 宏中不存在时输入默认图片

Inputting a default picture for when the called image doesn't exist in ImageJ macro

我创建了一个调用并打开 54 张不同图像的宏,这样我就可以将它们堆叠起来。这些图像都存储在一个文件夹中。有很多像第一个这样的文件夹,宏看起来能够从任何文件夹中的图像创建堆栈。我已经按照以下方式编写代码来执行此操作,因为这是将堆栈顺序与我希望图像全部成为的模式相关联的最简单方法:

open(image 1)
open(image 2)
open(image 3)
open(image 4)
open(image 5)
open(image 6)
open(image 7)
open(image 8)
open(image 9) ect.

但是,在某些文件夹中,某些图像(例如图像 8)不存在。这会在该点停止代码。我不想为每个 "open": "if file exists"... 写,因为我需要为 54 张图像写。有没有写法:

For this block of code, input default image if image doesn't exist for any of the following images:
    open(image 1)
    open(image 2)
    open(image 3)
    open(image 4)
    open(image 5)
    open(image 6)
    open(image 7)
    open(image 8)
    open(image 9) ect.
End for

如果我对你的问题的解释正确,你的 "image 1" 在你要处理的每个文件夹中都有相同的名称。

如果是这样,我认为您可以将 54 个可能的文件名列表放在 array 中。然后循环遍历数组索引,其中:

  1. 生成路径名并使用File.exists(path) (macro functions)
  2. 检查相应文件是否存在
  3. 如果存在,打开;如果不存在,请打开您的默认图像。