Macro for Automatic saving and filename designation

This is a macro that helped me out by allowing me to create several images from a single large image on ImageJ. I was wondering if someone could help me figure out how to automatically save those images as tiff’s and create individual file names for those images that are cropped from the single large image.

I have copy and pasted the initial code that was written. What additions will I need to make in order to get this accomplished?

Here is a macro that chops an image into NxN tiles, where N is the number
of divisions you choose:

n = getNumber("How many divisions (e.g., 2 means quarters)?", 2);
id = getImageID();
title = getTitle();
getLocationAndSize(locX, locY, sizeW, sizeH);
width = getWidth();
height = getHeight();
tileWidth = width / n;
tileHeight = height / n;
for (y = 0; y < n; y++) {
  offsetY = y * height / n;
  for (x = 0; x < n; x++) {
    offsetX = x * width / n;
    selectImage(id);
    call("ij.gui.ImageWindow.setNextLocation", locX + offsetX, locY + offsetY);
    tileTitle = title + " [" + x + "," + y + "]";
    run("Duplicate...", "title=" + tileTitle);
    makeRectangle(offsetX, offsetY, tileWidth, tileHeight);
    run("Crop");
  }
}
selectImage(id);
close();

All you need to do is use getDirectory("image") to store the pathname of the current open image – something like

path = getDirectory("image");

To save in the same directory you use:

selectWindow(tileTitle);
saveAs("tiff",path+tileTitle);

3 Likes

I’ll try it out, thanks so much. I appreciate your help. I’m a novice at writing code in these macros. greatly appreciated!

So I ran the modified macro, and this is the filename that was created. - large image.tif [2,0

It seems to have cropped the image properly, created a file within the existing folder, however, the file name with the coordinates after .tiff are not allowing us to open the file properly.

Ah, yes, to get the saving to work right, I think we have to strip the extension. I went back to the mailing list archives to get some hints for that. This may not work if you have extra dots in your filename!

n = getNumber("How many divisions (e.g., 2 means quarters)?", 2);
id = getImageID();
title = getTitle();
dotIndex = indexOf(title, ".");
basename = substring(title, 0, dotIndex);
path = getDirectory("image");
getLocationAndSize(locX, locY, sizeW, sizeH);
width = getWidth();
height = getHeight();
tileWidth = width / n;
tileHeight = height / n;
for (y = 0; y < n; y++) {
  offsetY = y * height / n;
  for (x = 0; x < n; x++) {
    offsetX = x * width / n;
    selectImage(id);
    call("ij.gui.ImageWindow.setNextLocation", locX + offsetX, locY + offsetY);
    tileTitle = basename + "[" + x + "," + y + "].tif";
    run("Duplicate...", "title=" + tileTitle);
    makeRectangle(offsetX, offsetY, tileWidth, tileHeight);
    run("Crop");
    selectWindow(tileTitle);
    saveAs("tiff",path+tileTitle);
    }
  }
selectImage(id);
close();

1 Like

This is the new error that I’m receiving upon running the macro.

Sorry it didn’t work yet! This error comes from the space in the original filename.

Archives to the rescue again:

n = getNumber("How many divisions (e.g., 2 means quarters)?", 2);
id = getImageID();
title = getTitle();
dotIndex = indexOf(title, ".");
basename = substring(title, 0, dotIndex);
path = getDirectory("image");
getLocationAndSize(locX, locY, sizeW, sizeH);
width = getWidth();
height = getHeight();
tileWidth = width / n;
tileHeight = height / n;
for (y = 0; y < n; y++) {
  offsetY = y * height / n;
  for (x = 0; x < n; x++) {
    offsetX = x * width / n;
    selectImage(id);
    call("ij.gui.ImageWindow.setNextLocation", locX + offsetX, locY + offsetY);
    tileTitle = basename + "[" + x + "," + y + "].tif";
    // using the ampersand allows spaces in the tileTitle to be handled correctly 
    run("Duplicate...", "title=&tileTitle");
    makeRectangle(offsetX, offsetY, tileWidth, tileHeight);
    run("Crop");
    selectWindow(tileTitle);
    saveAs("tiff",path+tileTitle);
    }
  }
selectImage(id);
close();
3 Likes

So it is creating the files properly, but still having issues with saving the images with “.tif” - as you can see by the highlighted files.

Thanks so much for trouble shooting this with me.

Hmmm, not sure what’s the problem here. I copied and pasted the entire code from my most recent post into the macro window, and it saved the images with the .tif extension.

A couple of iterations back I changed the tileTitle definition to append “.tif”. Perhaps you are using an earlier version?

So I uninstalled and re-installed ImageJ and… it worked!
Just wanted to thank you again for helping me figure that out.
Can’t thank you enough.

1 Like