Ashley Tucewicz
Ashley Tucewicz

Reputation: 1

ImageJ macro help: processing images differently according to file name

I'm new to ImageJ, but am trying to learn more about writing macros.

I have images that end in "10Xred.tif", "10Xgreen.tif", "10Xblue," and "10Xlr.tif". I would like to process each color using different parameters.

The code that I have written only transfers all files from the input folder to the output folder, without processing them. I'm not sure where the problem is.

My apologies if this question might be tedious or unsuitable for forum purposes - any and all advice would be very much appreciated!

Thank you.

input = getDirectory("Input directory");
output = getDirectory("Output directory");

Dialog.create("File type");
Dialog.addString("File suffix: ", ".tif", 5);
Dialog.show();
suffix = Dialog.getString();

processFolder(input);

function processFolder(input) {
    list = getFileList(input);
    for (i = 0; i < list.length; i++) {
        if(File.isDirectory(list[i]))
            processFolder("" + input + list[i]);
        if(endsWith(list[i], suffix))
            processFile(input, output, list[i]);
    }
}


function processFile(input, output, file) {

title = (File.getName(input));    
        if (indexOf(title, "10Xblue") >= 0) {
            run("Channels Tool...");
            run("Blue");
            run("Subtract Background...", "rolling=50");
            run("Multiply...", "value=1.2");
            run("Set Scale...", "distance=1 known=0.65 pixel=1 unit=µm global");
            run("Scale Bar...", "width=100 height=5 font=18 color=White background=None location=[Lower Right] bold overlay");
            run("RGB Color");
            run("Flatten"); }
        else if (indexOf(title, "10Xred") >= 0) {
            run("Channels Tool...");
            run("Red");
            run("Subtract Background...", "rolling=50");
            run("Multiply...", "value=1.350");
            run("Scale Bar...", "width=100 height=5 font=18 color=White background=None location=[Lower Right] bold overlay");
            run("RGB Color");
            run("Flatten");}
        else if (indexOf(title, "10Xgreen") >= 0) {
            run("Channels Tool...");
            run("Green");
            run("Subtract Background...", "rolling=50");
            run("Multiply...", "value=1.250");
            run("Scale Bar...", "width=100 height=5 font=18 color=White background=None location=[Lower Right] bold overlay");
            run("RGB Color");
            run("Flatten"); }
        else if (indexOf(title, "10Xlr") >= 0) {
            run("Channels Tool...");
            run("Magenta");
            run("Subtract Background...", "rolling=50");
            run("Multiply...", "value=1.200");
            run("Scale Bar...", "width=100 height=5 font=18 color=White background=None location=[Lower Right] bold overlay");
            run("RGB Color");
            run("Flatten");
        }


    print("Processing: " + input + file);
    open(input + file);
    print("Saving to: " + output);
    saveAs("TIFF", output+file);
    close();
}

Upvotes: 0

Views: 1591

Answers (1)

bogovicj
bogovicj

Reputation: 363

Welcome (I'm new too).

I see two issues. Match the filename patterns with the file variable, rather than input (input is a folder name here).

Second, and more importantly, the processing steps (those if/else-if statements) need to go in between the:

open(input + file);

and the

saveAs("TIFF", output+file);

Here's a modified version of your code that gets the idea across.

function processFile(input, output, file) {

    print("Processing: " + input + file);
    open(input + file);

    if (indexOf(file, "boats.tif") >= 0) {
        run("Multiply...", "value=0.2");
    }

    print("Saving to: " + output);
    saveAs("TIFF", output + file);
    close();
} 

Upvotes: 1

Related Questions