Macro for cropping bulk of images

Hii all,
I want to crop a bulk of images(approximately 500+images). For this, I want to write(or record) a macro which I can run on all images in the folder. The problem is that ROI for all images is different. So, how can I write a macro that enables me to select different ROI for each image,crop that ROI from image and then perform a series of commands on the cropped region. I have an idea. Can I take four points selected by mouse click in the function parameter , and then we would make rectangle using all those 4 points to crop the rectangle. Though I don’t have idea of how to take points using mouse click inside the function parameters. Any advice on this idea or some other idea would be very useful.


Hi @Arnav_Aakash,

When it’s time for the cropping step in your macro I would use the waitForUser command. So something like.

waitForUser(“Select region for cropping. \nClick ‘OK’ when done.”);

This pauses the macro until the user clicks the OK button on the dialog.

Thanks @lmurphy for the reply,
I also want to ask can we use waitForUser command in Batch Macro? As in batch Macro, we just input folder containing images, how to open each image in the folder to make a selection.(By the way, I am very new to ImageJ processing, so this question might be very trivial:))

Hi @Arnav_Aakash,

I assume you mean using the Batch Processor. I’m afraid I think it won’t work because if you use that the commands are run in Batch Mode which means the images don’t come up while they are being processed.

Therefore, if you put this command in the batch processor you would see the message to the user with the OK button but you wouldn’t see the image to draw on it which seems quite important!

So I’m afraid it will have to be a full macro but it’s really not that much more complicated that what you’re doing now. In fact if you open the macro editor (Plugins > New > Macro) then go to Templates > Image J 1.x > Batch > Process Folder (IJ1 Macro) then the script you get is essentially the batch processor you use but as a macro. You would just need to add the processes you want (including the waitForUser command) in the “processFile” section. And if you get stuck then either I or other users on the forum will be happy to help you get it to work!

Good luck!

Hi @Arnav_Aakash,

If, as indicated by @lmurphy you want to do this image-by-image in a non-batch manner, then possibly this annotated macro can give you a start. It works the user through a folder, one image at a time:

Image ROI_folder.txt (1.1 KB)

In the past I have found it useful to send two ROIs to the ROI Manager instead of just one. The first keeps the co-ordinates of the original, while the second keeps the co-ordinates of the the cropped image. Remember to select, then save, all the ROIs in the ROI Manager before closing it because you’re bound to need them again later!


1 Like

Thanks @lmurphy for the help. I got it with the help of your advice.Sorry for the late reply though:). And thanks for the macro @Bethany. Though I got it before, this can come handy for me in future too. :))

1 Like