Shuffled Image overlay

I have been using macros for imageJ to perform image analysis for about a year now. After the last update, all of my codes that incorporated batch mode began shuffling up image orders when using the overlay command (for example: Image 1a is suddenly being overlaid with Image 3c).

It seems to be systematically switching up files too so that if I run two different codes on the same sets of images the mismatch is maintained between codes. However, if I switch between files (which contain sets of images), the pattern by which the images are shuffled changes.

For example, I have images from 9Feb18. Images 1a through 1c overlay perfectly. However, at image 2a, image 3c is overlaid. Image 2b is then overlaid with 3b, 2c with 3a, 3a with 2c and so on. Meanwhile for 13Oct17 the image 1a is mismatched while 1B is matched properly. I have verified that these patterns are maintained in two independent codes, although the coding for their batch processing is near identical.

I am running Java 1.8.0_66 and ImageJ 2.0.0 on a Mac OS X system.

Below is a copy of a minimized version of my code , which I have verified creates the same problem (although interestingly enough, it seems to create its own shuffling pattern, whereas my other two codes share a pattern). My apologies if it isn’t the prettiest or most logical. Everything I know was learned from writing the above mentioned codes.

I will also attach the same images that I have been using (new users can only add 5 images, the but the images sometimes stay in order if only three or less are used). The images will need to be put in a folder by category (background images: contain BF in their title; and overlay images: contain GFP in their title). A separate folder is also needed to save the overlaid images. They can then be compared to the images you overlaid for order.

overlayQ = (getBoolean("Would you like to Overlay images?"));

    if(overlayQ == true)
          title = "Instructions";
          msg = "Select the background images";
          waitForUser(title, msg);

              base_image_directory = getDirectory("Choose Source Directory ");

          title = "Instructions";
          msg = "Select the File Containing the images you want to overlay";
          waitForUser(title, msg);

              overlay_image_directory = getDirectory("Choose Source Directory ");

          title = "Instructions";
          msg = "Select the File where you would like to save the images after you have edited them";
          waitForUser(title, msg);

              destination_directory = getDirectory("Choose Destination Directory ");

    title = "Untitled";
    width=1050; height=512;
    Dialog.create("Title Adjustment");
    Dialog.addString("Create a label to distinguish the edited image from the original:", "Edited");
    Dialog.addCheckbox("Yes?", true);;

    name= Dialog.getString();

    base_images = getFileList(base_image_directory);
      overlay_images = getFileList(overlay_image_directory);

      for (i=0; i < base_images.length; i++) 
      showProgress(i+1, base_images.length);
        base_image_id = getImageID();
          overlay_image = overlay_images[i];


        run("Add Image...", "image=&overlay_image x=0 y=0 opacity=75");
                            saveAs("TIFF", destination_directory+name+base_images[i]);

1 Like

Background Images (1-5)


Background Images (6-9)


Overlay Images (1-5)


Overlay Images (6-9)


Hi Ryan,

Welcome to the forums!

Can you insert the following code in to your macro after you use the getFileList function for both directories:;;
waitForUser("Check file lists");

I get the following:

As far as I’m aware, that is the order that your images will be loaded in starting from 0 (first file in the list). For me, the macro works as expected.

Maybe your file order is incorrect for some reason?




Hi Rob,

Thanks for the help, it seems to be that my files aren’t in order.

I’m not sure exactly how to fix this though. This is how the files appear in the directory:

This should be the correct order, although I suppose how this order is viewed can be changed easily. How to I alter the order in which the files are opened? I haven’t moved or changed any settings related to these files between the time that the code was working and now.


Hi Ryan,

I always use the macro function Array.sort(array) after I use the getFileList function. My experience is that the fileorder is shuffled often especially when the folder contains many images.
I hope this will help you!

Regards, Peter


I tried using the array.sort(array) function, but I was still having the same issue. I ultimately decide to just delete my copy of FIJI and redownload it. The minimal program is working again and I will check the full program tomorrow. Something must have downloaded improperly when I updated last.

@PeterRZ and @7rebor thank you very much. I appreciate the help more than I can express over a screen.


1 Like

It appears that this problem has also been encountered in the ImageJ mailing list here. Something to do with the new Mac OS not sorting files, updating your ImageJ might have fixed it.

Glad you solved it!


1 Like