Search code examples
imagej

Running fiji/imagej macro from terminal


I have made a macro in fiji/imagej that i would like to activate via the terminal in a shell script. As it now stands, the macro does not need any inputs, I just want to make fiji run the macro when activated from the terminal, and save its output in the output folder. The script for the macro looks like this:

input =  "/Users/matsboh/Documents/PhD/Experiment/Image analyses/Raw/";
output =  "/Users/matsboh/Documents/PhD/Experiment/Image analyses/Results/";

setBatchMode(true); 
list = getFileList(input);
for (i = 1; i == list.length; i++){

var im = i;

name = "raw_" + im + ".jpg"; 
dir_name = input + name;

open(dir_name);


run("Subtract Background...", "rolling=50 light");
run("8-bit");

run("Make Binary");

name = "results_" + im + ".csv"; 
dir_name = output + name;

run("Analyze Particles...", "size=100-20000 show=Nothing display clear");
saveAs("Measurements", dir_name); 
close();

}

If this is possible, how would i precede?

Cheers,

Mats


Solution

  • On OS X:

    /Applications/Fiji.app/Contents/MacOS/ImageJ-macosx --headless -macro myAwesomeMacro.ijm
    

    On Windows:

    %USERPROFILE%\Fiji.app\ImageJ-win64.exe --headless -macro myAwesomeMacro.ijm
    

    On Linux:

    ~/Fiji.app/ImageJ-linux64 --headless -macro myAwesomeMacro.ijm
    

    Personally, I like to alias the full path to my ImageJ installation's launcher to either imagej or fiji so I can then just type:

    imagej --headless -macro myAwesomeMacro.ijm
    

    Of course, how you do that aliasing will also depend on your OS.

    See the Headless page of the ImageJ wiki for further details on running macros and scripts from the console.