Apply functions based on image sizes

Hi there, I’m new to Fiji but have some experience coding in Matlab so sorry if this has been already asked.

Basically, I have a lot of images I want to batch process. The main difference in the images is the amount of pixels in each one (binning). I have a macro that can process the files but the user needs to select them which is open to error and slow. Is there a function that will scan the files and determine which one to open or apply a function to based on size? Ideally this wouldn’t open all the images in the folder, trying this on a laptop and don’t want to run out of RAM.

Open to any suggestions and thanks in advance!

Hey @Ben_Mc_Larney,

so you can go through a folder, open one image after the other and ask ImageJ for the image size in a macro and make decisions based on that.

// let the user select a folder
filelist = getFileList(directory) 

// go through that folder
for (i = 0; i < lengthOf(filelist); i++) {
    // check if a given file is a tif image
    if (endsWith(filelist[i], ".tif")) { 
        // open the image
        filename = filelist[i];
        open(directory + File.separator + filename);

        // check the image size
        imageWidth = getWidth();
        imageHeight = getHeight();
        if (imageHeight < 1024 || imageWidth < 1024) {
            print(filename + " is a small image");
        } else {
            print(filename + " is a large image");
        }

        // close the image
        close();
    } 
}

Let us know if this helps!

Cheers,
Robert

Dear Ben,
You can use the Bio-format extension if you don’t want to open the file to check the size of your image.

Ext.setId(inputPath2);
Ext.getSizeX(width);
Ext.getSizeY(height);

run("Bio-Formats Macro Extensions");	
function processFolder(input);
list = getFileList(input); 
	for (j=0; j<list.length; j++) {
		if (endsWith(list2[j],".tif")){
			inputPath= input +  list2[j];
			Ext.setId(inputPath);
			Ext.getSizeX(width);
			Ext.getSizeY(height);
}

Hey Robert and Alex,

Thanks a million for your help and the code! Will give each a go and let you know how I get on.

Best,

Ben