Batch processing -> "there are no images open" issue


#1

Hey everyone,

Using the built-in batch process macro combined with one recorded macro, I end up getting this issue “there are no images open”.

Any help would be really appreciated

// "BatchProcessFolders"
//
// This macro batch processes all the files in a folder and any
// subfolders in that folder. In this example, it runs the Subtract 
// Background command of TIFF files. For other kinds of processing,
// edit the processFile() function at the end of this macro.
//
//

   requires("1.33s"); 
   dir = getDirectory("Choose a Directory ");
   name2=getTitle; 
   folder=getDirectory("image"); 

   setBatchMode(true);
   count = 0;
   countFiles(dir);
   n = 0;
   processFiles(dir);
   //print(count+" files processed");
   
   function countFiles(dir) {
      list = getFileList(dir);
      for (i=0; i<list.length; i++) {
          if (endsWith(list[i], "/"))
              countFiles(""+dir+list[i]);
          else
              count++;
      }
  }

   function processFiles(dir) {
      list = getFileList(dir);
      for (i=0; i<list.length; i++) {
          if (endsWith(list[i], "/"))
              processFiles(""+dir+list[i]);
          else {
             showProgress(n++, count);
             path = dir+list[i];
             processFile(path);
          }
      }
  }

  function processFile(path) {
       if (endsWith(list[i], "IHC.tif")) { /// my recorded macro starts here
			open(path);
			call("ij.ImagePlus.setDefault16bitRange", 16);
			run("Appearance...", "antialiased menu=15 16-bit=[16-bit (0-65535)]");
			run("Properties...", "channels=1 slices=1 frames=1 unit=pixel pixel_width=1 pixel_height=1 voxel_depth=1 global");
			//run("Brightness/Contrast...");
			setMinAndMax(750, 65535);
			call("ij.ImagePlus.setDefault16bitRange", 16);
			run("Apply LUT");
			saveAs("tiff", folder + name2 + "_sans_background");
			close();

      }
  }

#2

Good day,

not sure what

name2=getTitle;

means. Perhaps you remove this code line.

HTH

Herbie


#3

Hey @Moximatoz

I think the problem is coming from your name2=getTitle; call - as there is no open image to get the title of…

If I were you - I would go back to this previous reply to your original post on the forum… Using Script Parameters can really simplify your code.

So your code would look like this:

// @File(label = "Input directory", style = "directory") input
// @File(label = "Output directory", style = "directory") output
// @String(label = "File suffix", value = "IHC.tif") suffix

processFolder(input);

// function to scan folders/subfolders/files to find files with correct suffix
function processFolder(input) {
	list = getFileList(input);
	list = Array.sort(list)
	for (i = 0; i < list.length; i++) {
		if(File.isDirectory(input + list[i]))
			processFolder("" + input + list[i]);
		if(endsWith(list[i], suffix))
			processFile(input, output, list[i]);
	}
}

function processFile(input, output, file) {
	run("Bio-Formats", "open=[" + input + "/" + file + "] autoscale color_mode=Default rois_import=[ROI manager] view=Hyperstack stack_order=XYCZT");
	call("ij.ImagePlus.setDefault16bitRange", 16);
	run("Appearance...", "antialiased menu=15 16-bit=[16-bit (0-65535)]");
	run("Properties...", "channels=1 slices=1 frames=1 unit=pixel pixel_width=1 pixel_height=1 voxel_depth=1 global");
	//run("Brightness/Contrast...");
	setMinAndMax(750, 65535);
	call("ij.ImagePlus.setDefault16bitRange", 16);
	run("Apply LUT");
	saveAs("tiff", output + file + "_sans_background");
	close();
}

NOTE: I have not tested this code fully - you need to test it on your own datasets to be sure it’s working properly…

Hope this helps.

eta


#4

Thanks you both, I finally succeeded using this macro:
(I’m not sure if it would work with subfolders but in my case all the images are in the same directory.)

dir1 = getDirectory("Choose Source Directory ");
dir2 = dir1;
list = getFileList(dir1);

setBatchMode(true);
for (i=0; i<list.length; i++) {
 showProgress(i+1, list.length);
 filename = dir1 + list[i];
 if (endsWith(filename, "IHC.tif")) {
 open(filename);
 

call("ij.ImagePlus.setDefault16bitRange", 16);
run("Appearance...", "antialiased menu=15 16-bit=[16-bit (0-65535)]");
run("Properties...", "channels=1 slices=1 frames=1 unit=pixel pixel_width=1 pixel_height=1 voxel_depth=1 global");
//run("Brightness/Contrast...");
setMinAndMax(750, 65535);
call("ij.ImagePlus.setDefault16bitRange", 16);
run("Apply LUT");


 saveAs("TIFF", dir2+list[i]+"_sans_background");
 close();
 }
}