Save all opened images

Hi everyone, I am trying to save all opened images with their original names. I am using a macro code:

// get image IDs of all open images 
dir = getDirectory("Choose a Directory"); 
for (i=0;i<nImages;i++) { 
        title = getTitle; 

        saveAs("tiff", dir+title); 

It worked pretty well several months before, but somehow this time all images were saved as empty file (the size is 0, and not in tiff format). Is there something wrong with the language that I selected (I tried Java, IJ1macro and Beanshell, none worked). Can anyone nicely help me or give a clue? Many many thanks.

1 Like

I don’t know what may be going wrong. I’ve been using a slightly modified version of that code for years without problems. Here you have the version I use in case you want to give it a try:

// Obtenido de

// get image IDs of all open images
dir = getDirectory("Choose a Directory");
for (i=0;i<nImages;i++) {
        title = getTitle;

        saveAs("tiff", dir+title);
run("Close All");

BTW, it will close all images when it is done


Thank you so much for your reply! Do you know at which language this macro is applied (if you click ‘language’ in the Macro window)? Thank you in advance.

I saved it as .ijm, which would be IJ1macro.

Thank you for your help. Is that possibly due to not enough memory?

I don’t think so. You should get a message if that were the problem. Anyway, you can check how much memory you have allocated to the program in Edit>Options>Memory & Threads. And you can check how much you are using in Plugins>Utilities>Monitor Memory

You are right, it’s not the memory problem. I think I know the problem. My initial images are named as “1: ImageA”, and it turned out if I change the name to “ImageA” then the macro code worked perfectly. Do you know the macro to remove “1:” (the number plus colon) in the image name? Many thanks.

Yes, probably the colon in the name is the problem. You can use the following command to replace it for a “_”

replace( title, ":", "_" );

You can assign it to a variable and then use that variable in the “saveAs” path.

Alternatively, you can use a different software to batch rename all your files before you run the macro. I have used Lupas Rename 2000 previously and it works beautifuly. It is freeware.

I hope you can get it to work

The colon is, or has been, a directory separator in some operating systems. Sometimes they are shown as slashes (/) but underneath they are colons. Hence forbidden in file names, to prevent confusion.

Hi, this macro only seems to save my first opened image rather than saving all - do you know why this might be?


Shot in the dark: might it be that all your images have the same title and the next image overwrites the previous, having you ending up with only one image? Single stepping the macro code in Debug mode (ImageJ1 only) may reveal the problem you are encountering.

Here is an ImageJ-macro function that gives unique name to all open images:

function uniqueName( name_array ) {
   Version: Herbie G., 03. Dec. 2011
   Comment: gives unique names to all open image window
   Input parameter: empty array, e.g. imgNameArray = newArray( nImages );
   Output parameter: imgNameArray filled with file names
   k = name_array.length;
   for ( i = 0; i < k; i++ ) {   // loop image window menu list
      selectImage( i + 1 );
      new_title = getTitle();
      for ( j = 0; j < i; j++ ) {   // check for doublets
         if ( name_array[j] == new_title ) {
            name_comp = split( new_title, "." );
            new_title = name_comp[0] + "_#" + i;
            if ( name_comp.length > 1) { new_title += "." + name_comp[1]; };
         rename( new_title );
      name_array[i] = new_title;




This code always leaves one image out, I do not understand why. It makes sense to set i=0 and selectImage(i+1) but one image is still left out.

Any clue?


Hi Fepu,

If you are talking about the code I sent, it works fine for me. I just retested it and it saved all the images open. Could it be that the name of your images is just a number? Maybe that could be an issue since selectImage(id) works in the following way:

“Activates the image with the specified ID (a negative number). If id is greater than zero, activates the id th image listed in the Window menu. The id can also be an image title (a string).”

I hope this helps

Rather than wrapping the saving code in a for loop, wrap it in a while or do loop.

While( the number of images that are open is not zero){
increase counter, saveas (title + counter);
close the window
Do you manage with the while link and the pseudo code?

Continuing the discussion from Save all opened images:

Thanks so much for sharing the code, Javier. It worked well until I hit image 18. Then, it says that image 19 does not exist/it is not able to find image 19. Why would this occur? Is there a limit to how many images IJ catalogs? I have 36 images open that I want to save iteratively. I would appreciate any help.

Do you by chance close the images within the loop? That would explain why you only succeed with half of your images (nImages will be different in each loop iteration, and once you closed the 18th image, there are only 18 left open and the 19th cannot be found.)

In the original script, the images are closed after the loop. If you want to close them within the loop, it’s safer to start from the other end:

for (i = nImages; i > 0; i--) {


I notice that this topic is among the most seen topics (6.6k views in the last 2.5 years).

Maybe it would be a good idea to have a save-all-open-images function in the Save Menu ?

I’m sure that many non macro writers will find it useful.

@imagejan @Wayne

Thanks a lot

The ImageJ 1.53h28 daily build adds the following macro to the Help>Examples>Macro menu. To create a “Save All Images” command, save this macro in the plugins folder, or subfolder, as “Save_All_Images.ijm”.

// Saves all open images in TIFF format.
// Uncomment last line to close the saved images.
// Save this file in the plugins folder, or subfolder,
// to create a "Save all Images" command.

  if (nImages==0)
     exit("No images are open");
  dir = getDirectory("Choose a Directory");
  for (n=1; n<=nImages; n++) {
     title = getTitle;
     saveAs("tiff", dir+title.replace("/","_"));

You will also want to check out, in the daily build, the amazing Help>Examples>Macro>Colors of 2021 animation.