NICOLAS ROA
NICOLAS ROA

Reputation: 1

Assistance Needed with ImageJ Macro for Processing TIFF Images in Subdirectories

I'm working on an ImageJ macro with the following goals: 1.-Open an input directory. 2.-Create an output directory. 3.-Search for directories named "BRIGHT" across multiple levels of subfolders, pre-process TIFF images within these directories, and copy them to the output directory.

However, when I run the macro, it only creates an empty folder in the output directory without performing any of the intended actions. It seems that the script isn't recognizing the instructions or executing them properly.

Below is the macro I'm using:

// select folder with "BRIGHT" images
inputDir = getDirectory("Choose your input directory"); // `inputDir` stores the path of the input directory

// select output directory
outputDir = getDirectory("Choose your output directory"); // `outputDir` stores the output path
newFolderName = getString("Name of the new folder:", "NewFolder");
newDir = outputDir + File.separator + newFolderName; // `newDir` stores the path of the output folder
File.makeDirectory(newDir);

setBatchMode(true); // batch processing, avoids updating the interface in each operation

// To search for BRIGHT at any level

function searchAndProcess(inputDir, parentPath) {
    if (inputDir == null || inputDir.length() == 0) { // check if the directory is empty
        print("Invalid or empty directory.");
        return;
    }
    
    fileList1 = getFileList(inputDir); // `fileList1` stores a list of files in the BRIGHT folder
    for (i = 0; i < fileList1.length; i++) { // create a loop
        currentPath = inputDir + File.separator + fileList1[i]; // stores the full path for each file in `currentPath`
        showProgress(i, fileList1.length); // shows a progress bar
        
        if (File.isDirectory(currentPath)) { // check if `currentPath` is a directory
            if (matches(fileList1[i], "*BRIGHT*")) {
                // Create a subfolder in the destination for this BRIGHT folder
                brightSubDir = newDir + File.separator + (parentPath.length() > 0 ? parentPath + "_" : "") + fileList1[i];
                if (!File.exists(brightSubDir)) {
                    File.makeDirectory(brightSubDir);
                    
                    // List and process the TIFF files in the BRIGHT folder
                    brightFiles = getFileList(currentPath);
                    for (j = 0; j < brightFiles.length; j++) {
                        sourceFile = currentPath + File.separator + brightFiles[j];
                        if (!File.isDirectory(sourceFile) && endsWith(brightFiles[j], ".tif")) { // pre-processing
                            open(sourceFile);
                            run("8-bit");
                            run("Duplicate...", "duplicate");
                            saveAs("Tiff", brightSubDir + File.separator + brightFiles[j]); 
                            close(); // Closes the duplicated image
                        }
                    }
                }
            } else { // Recursion: search in subdirectories
                newParentPath = (parentPath.length() > 0) ? parentPath + "_" + fileList1[i] : fileList1[i];
                searchAndProcess(currentPath, newParentPath);
            }
        }
    }
}

print("TIFF files processed and saved in: " + newDir);

Has anyone successfully tackled a similar task or could offer some guidance on where the issue might be?

Upvotes: 0

Views: 110

Answers (2)

Herbie
Herbie

Reputation: 328

The below ImageJ-macro code should do the job:

//imagej-macro "batch_selectiveDirProc" (Herbie G., 03. Sept. 2024)
requires("1.54j");
close("*");
var cnt=0;
inSfx=".tif";
Dialog.create("Batch Process <Bright>-Folders");
   Dialog.addDirectory("Source-Folder",getDir("file"));
   Dialog.addDirectory("Result-Folder",getDir("file"));
Dialog.show();
srce=Dialog.getString();
dest=Dialog.getString();
setBatchMode(true);
processFolder(srce,dest,inSfx);
setBatchMode(false);
exit(""+cnt+" TIF-files processed");
function processFolder(in,out,sfx) {
   list = getFileList(in);
   for (i=0;i<list.length;i++) {
      path=in+list[i];
      if (endsWith(list[i],"/"))
         processFolder(path,out,sfx);
      else if (endsWith(list[i],sfx)&&endsWith(File.getDirectory(path),"BRIGHT/"))
         processImage(path,out+list[i],sfx);
   }
}
function processImage(inPth,outPth,ext) {
   open(inPth);
   print(inPth);
     // add code here to process the image
   saveAs(ext,outPth);
   close();
   cnt++;
}
//imagej-macro "batch_selectiveDirProc" (Herbie G., 03. Sept. 2024)

Upvotes: 0

Armali
Armali

Reputation: 19375

Has anyone successfully tackled a similar task or could offer some guidance on where the issue might be?

There are multiple issues.

  • As quantixed stated, searchAndProcess isn't called. If it is, you'll notice some errors.

  • There no null in the ImageJ macro language. The comparison with null in the macro isn't needed, so you can drop it.

  • The matches function operates with a regular expression, and *BRIGHT* is not a legal one. You can use e. g. BRIGHT/, BRIGHT. or a plain comparison.

  • The length of a string is determined by lengthOf(string). The string length tests in the macro aren't needed, so you can drop them.

Upvotes: 0

Related Questions