Never stop batch

Hi CP,

I have difficulties on my batch code.
I try to convert JPG to TIF and analyze tif files for several files in a folder.
My batch code works pretty good but it is never end.
After all process, a popup window open to choose a folder.
Is there any point that I missed?
My batch code is below. Please let me know what I missed.

thanks for your help !!

dir1 = getDirectory("Choose Source Directory "); 
format = getFormat();
dir2 = getDirectory("Choose Destination Directory "); 
list = getFileList(dir1);
setBatchMode(true);
for (i=0; i<list.length; i++) {
 showProgress(i+1, list.length);
 open(dir1+list[i]);
	
UserDefColors();  
if (format=="8-bit TIFF" || format=="GIF")  
 convertTo8Bit();
 saveAs(format, dir2+list[i]);  //saves the image file
 close();
}
	
macro "Batch Measure" {
    dir = getDirectory("Choose a Directory ");
    list = getFileList(dir);
    if (getVersion>="1.40e")
        setOption("display labels", true);
    setBatchMode(true);
    for (i=0; i<list.length; i++) {
        path = dir+list[i];
        showProgress(i, list.length);
        if (!endsWith(path,"/")) open(path);
        if (nImages>=1) {
            run("Measure");
            close();
        }
    }
}
		
function getFormat() {
 formats = newArray("TIFF", "8-bit TIFF", "JPEG", "GIF", "PNG",
 "PGM", "BMP", "FITS", "Text Image", "ZIP", "Raw");
 Dialog.create("Batch Convert");
 Dialog.addChoice("Convert to: ", formats, "TIFF");
 Dialog.show();
 return Dialog.getChoice();

}

function convertTo8Bit() {
 if (bitDepth==24)
 run("8-bit Color", "number=256");
 else
 run("8-bit");

}

function UserDefColors()

{

min=newArray(3);
max=newArray(3);
filter=newArray(3);
a=getTitle();
run("HSB Stack");
run("Convert Stack to Images");
selectWindow("Hue");
rename("0");
selectWindow("Saturation");
rename("1");
selectWindow("Brightness");
rename("2");
min[0]=0;
max[0]=45;
filter[0]="pass";
min[1]=0;
max[1]=110;
filter[1]="pass";
min[2]=110;
max[2]=255;
filter[2]="pass";
for (i=0;i<3;i++){
  selectWindow(""+i);
  setThreshold(min[i], max[i]);
  run("Convert to Mask");
  if (filter[i]=="stop")  run("Invert");
  
}

imageCalculator("AND create", "0","1");
imageCalculator("AND create", "Result of 0","2");
for (i=0;i<3;i++){
  selectWindow(""+i);
  close();

}

selectWindow("Result of 0");
close();
selectWindow("Result of Result of 0");
rename(a);

run("Fill Holes");
run("Remove Outliers...", "radius=20 threshold=1 which=Dark");
run("Analyze Particles...", "  show=Nothing summarize");

}
1 Like

Hey @skturf,

could you try commenting out the block which starts with macro "Batch Measure" { ? It appears there is a second loop running over your folders, which might not be intended :wink:

Cheers,
Robert

HI Robert,

Thanks for your comments!

You are absolutely right.
I remove the sentence you mentioned and tried again.
The code sentence you mentioned is not necessary.
Finally, It was solved.
The sentence you mentioned and codes below that were not necessary.

macro “Batch Measure” {
dir = getDirectory(“Choose a Directory “);
list = getFileList(dir);
if (getVersion>=“1.40e”)
setOption(“display labels”, true);
setBatchMode(true);
for (i=0; i<list.length; i++) {
path = dir+list[i];
showProgress(i, list.length);
if (!endsWith(path,”/”)) open(path);
if (nImages>=1) {
run(“Measure”);
close();

After remove those codes, it works very well.

Thanks for your help and this community is really helpful !

skturf

2 Likes