Skip to content

How to Start Using Coding to Automate Image Analysis Part 2: Batch Processing Multiple Images

Posted in: Software and Online Tools
How to Start Using Coding to Automate Image Analysis Part 2: Batch Processing Multiple Images

In Part 1 of this article, I introduced you to using code for basic image manipulation in ImageJ and working with the command recorder to expand your coding vocabulary. I covered how to make a simple macro, how to edit it and then save it to be run again another time. If you skipped the first part, but you’re comfortable with all that, then read on. Otherwise I suggest going back and starting there.

Whilst using macros to process single images can save you plenty of time (especially when you carry out several steps of manipulation or analysis on each image) they are most useful when you want for batch processing multiple images at once. Consider the example used in Part 1 where I just flipped the image and saved it in a different format. Even this simple image manipulation can become time consuming & tedious if you have hundreds of images to deal with. While it is possible to write macros which can handle multiple images, it is a little trickier; luckily, though, ImageJ has another tool which lets you do this with no extra coding knowledge.

The Batch Process Tool

The batch process tool (Process –> Batch –> Macro…) gives you the ability to run a macro on every image in a folder. It is so simple to use, you can just copy and paste code from other macros directly into it and the batch process tool will take care of an entire folder of images in one go.

Batch Process Tool

 

When you open the batch process tool you will see the window above. There are three things we need to enter in order to use this tool:

  1. The files to process: click the Input… button to select the folder containing the images that you want to be processed.
  2. Where to save the processed images: click the Output… button to select the folder where you want to save the resulting images. You can also change the file-type that they will be saved as using the Output Format drop-down list if you want to (TIFF is selected by default).
  3. The macro code itself: The main text box in the window is blank for you to add the code you wish to perform on each of the images.
    • The Add Macro Code drop-down list gives you some frequently used sample code to add into your macro, but otherwise you can just write your own.
    • As an example, I’m going to use my original code from Part 1:pict 4
    • But the batch process tool is designed to automatically handle the “open”, “saveAs” and “close” functions for you, so these can be removed from the code completely. This shortens my macro into just one line:

pict 5

Once these three things have been entered, you can use the Test button to preview exactly what the macro does to the first image without running through the whole folder. If this looks right, then you can now go ahead and click the Process button to run this macro on all the images. Just as for normal macros, batch macros can be saved and opened again for use at another time using the Save… and Open… buttons at the bottom of the batch process tool window.

In Closing

So here I’ve written a macro to flip and save as many images as I want in one click, and it only took a matter of seconds. All I had to do was copy the code over from a macro I created just as quickly using the command recorder. Granted, this is a very simple example, but it just illustrates how easy the process can be. The more familiar you become with writing code, the more complex macros you can create and by combining them with the batch tool you can see that you really don’t need much coding knowledge or experience to see significant benefits from using these techniques. The time savings are obvious, but the value of standardizing a complicated workflow to minimize human error should also not be overlooked.

Now, you’ve seen how simple it can be, you may want to play around further and start writing your own code from scratch. There are plenty of online resources and tutorials that are great for getting you started and I’ve included some nice ones at the end of this article. If you get on well with this kind of coding, you might also be interested to look into how it can help you in many other parts of your work – smarter spreadsheets & databases using VBA, advanced data manipulation using R, Matlab or SPSS, or revamp your blogs & webpages using JavaScript.

Finally, just remember this: it’s software, you can’t break anything, and if you keep backups of all your original images you can’t ruin your data either, so there’s no reason not to play around with code and see what you can do.

Helpful Links

Share this to your network:

Leave a Comment

You must be logged in to post a comment.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll To Top