Pat yourself on the back, you saw a post with the word “coding” in the title and you didn’t freak out or glaze over. That’s the first step.
Coding seems to have such a stigma attached to it; people tend to think that it’s incomprehensible nonsense that they could never learn, and that it has no practical use for the everyday scientist. Hopefully, by the time you get to the end of this article, you’ll see that this is not the case and be prepared to give it a go yourself.
Don’t worry, I’m gonna keep it simple though. Here, I’ll show you how you can use coding within ImageJ to automate image analysis, freeing up your time for more important things like trying to figure out who keeps stealing your buffers.
Part 1: The Simple Process
Coding 101
Not many people know that ImageJ comes with something called the “command recorder”. This is a handy little tool that follows every action you take within the program and spits out code that will replicate exactly what you just did, but in a single click. This means you can code without actually having to write a single bit of code yourself. So to anyone who says that they could never learn coding, I say you don’t even have to!
Choose a free resource to help you move forward
download
The Molecular Cloning Cheat Sheet
DOWNLOAD
Western Blot Troubleshooting Card
For now, let’s run through a simple example and I’ll explain some of the absolute basics of coding. I’m going to use the command recorder to write a macro (a collection of code) that will open a proprietary format image (.zvi), flip it horizontally, save it as a tiff and close it.
Every time I click a command, a new line appears in the command recorder window. At the end of the process, there are four lines of code which is all ImageJ needs to know to repeat that process again. Clicking the Create button exported this code to the macro editor window where we can work with it further.
Making Sense of the Code
So now, I’ve created a macro that looks like this:
But what does it mean? Let’s break that code down into its individual pieces to make it a little easier to understand:
- Each line contains a single action, with a semi-colon to mark the end of that action.
- Each action consists of a “function” and “parameters”.
- Functions are instructions telling ImageJ what to do – these are the first words on each line and are shown in red. They are followed by brackets containing their parameters.
- Parameters tell ImageJ how to do it – these are placed inside the brackets and are separated by commas. These are shown in blue. Functions often only have one parameter, but can have multiple or none at all.
Knowing this, you can see that in this example my macro uses the functions “open”, “run”, “saveAs” and “close”, each followed by parameters containing the extra information ImageJ needs to make those functions do exactly what I just did by clicking through the menus. To break them down further:
- The parameter for “open” is the directory/filename of the image that I opened.
- The parameter for “run” is the command I wanted to run (flip horizontally).
- The “saveAs” function has two parameters; the first is the file type to save as (tiff), and the second is the directory / filename where I wanted to save it.
- There are no parameters added to the “close” function. Any function that doesn’t have parameters assigned is carried out with its default parameters. The default parameter of the “close” function is the name of whatever window is selected at that time, so in my code this line will close the selected window.
If you have no coding experience, you would have no way of knowing those functions or parameters, but the command recorder shows you exactly what code is needed to replicate your actions. You could literally make your first macro in 30 seconds with no previous coding knowledge. It really is that simple.
Editing Macros
Now I have a macro to flip my images; the only problem is that the current parameters make it work specifically for Image-0001 in my Images folder. However, I can remove the parameters for “open” and “saveAs” so that it works for any file.
By removing the parameters which tell ImageJ which files to open from and save to, both the “open” and “saveAs” functions will ask me to pick a file name & location each time I run the macro, but then continue with everything else exactly as before.
To save this macro, I can select File –> Save from the macro editor window. Now I can open it to edit further or run this macro again using the Edit… and Run… commands in the Plugins –> Macros menu from the main ImageJ toolbar.
Summary
Obviously that is a very simple example and doesn’t really save much time, but the actions you can automate can be much more extensive than just flipping an image. The coding language is comprehensive enough to automate nearly any image processing or data extraction & manipulation with enough practice.
The command recorder is the simplest way to get to grips with the ImageJ coding language, as you can just play around with your images and watch the different commands pop-up on screen. The more you use it, the more you’ll see patterns emerging and the logical structure of the language.
The next section explaind how to further automate image analysis and process multiple images in one go with the batch process macro tool.
Part 2: Batch Processing Multiple Images
In part 1, 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.
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:
- The files to process: click the Input… button to select the folder containing the images that you want to be processed.
- 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).
- 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.
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.
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
You made it to the end—nice work! If you’re the kind of scientist who likes figuring things out without wasting half a day on trial and error, you’ll love our newsletter. Get 3 quick reads a week, packed with hard-won lab wisdom. Join FREE here.

