macrosimagejimagej-macro

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


I've created a macro that calls and opens 54 different images so that I can make a stack out of them. These images are all stored in one folder. There are many folders like the first, and the macro looks to be able to create a stack out of the images in any folder. I've written the code to do this in the following way, because it was the easiest way to relate the stack order to the pattern I want the images all to be:

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.

However, in some folders, some images, for example, image 8, don't exist. This stops the code at that point. I don't want to write, for each "open": "if file exists"... because I'd need to do that for 54 images. Is there a way of writing:

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

Solution

  • If I'm interpreting your question correctly, your "image 1" has the same name in each folder that you want to process.

    If so, I think you could put the list of 54 possible filenames in an array. Then make a loop through the array indices in which you:

    1. Generate a pathname and check if the corresponding file exists using File.exists(path) (macro functions)
    2. If it exists, open it; if it doesn't exist, open your default image.