How to use script editor and macros to batch resize a selection of images across several subdirectories

Hi,

I want to rescale/resize about 30 images and save these smaller copies in the same folder where the originals are found.

The original images are contained in their respective folders by sample, and I’d like the smaller copy to be saved in their corresponding folder as well.

Example:

So far I understand I need to use the Script Editor to build something like this, but I’m not sure how to actually implement this:

dir1 = getDirectory("Choose Source Directory ");
dir2 = getDirectory("Choose Destination Directory ");
list = getFileList(dir1);
setBatchMode(true);
for (i=0; i<list.length; i++) {
 showProgress(i+1, list.length);
 filename = dir1 + list[i];
 if (endsWith(filename, "tif")) {
 open(filename);

//MACRO to resize images HERE

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

Thank you!

Hi @carms,

I read your question like you want to learn doing this yourself, right?

Your code is a good starting point for going trough a folder and opening all images files. If you want to learn how to do the rest yourself, I would recommend activating the Macro Recorder in Fiji by clicking the menu Plugins > Macro > Record. Then, you open an image, scale it and save it. The recorder will record something like this:

image

This is the code you can enter in your program where it says “//MACRO to resize images HERE”.

Do you think, you can figure the rest out yourself?

Just one more hint: You may need the function getDimensions() to read the image dimensions and put the right values in the run… command. You can see what it does by starting to type it in the script editor and checking the auto-complete documentation:

Let us know if you need further hints or support.

Cheers,
Robert

1 Like

Thanks for your response, @haesleinhuepf ! That is very helpful.

Another part I don’t know how to implement is how to save the re-scaled images in the same folder as their original counterparts.

i.e., I don’t want all the output files to be saved in a dir2 as in the example, but rather for

02_14_orgA_SVCC_RAW_ch01_small.tif, for example,
to be saved in the same directory as
02_14_orgA_SVCC_RAW_ch01.tif.

Many thanks.

If you call this for opening:

filename = dir1 + list[i];
open(filename);

You can call this for saving:

filename = dir1 + replace(list[i], ".tif", "_small.tif");
saveAs("TIFF", filename);

Before testing such stuff, better make a backup of your folder - just in case :wink:

Cheers,
Robert

1 Like