Macro for bulk processing/cropping of images



Hi guys,

I am new to the world of macro programming, so please bear with me :wink:
I am trying to write a macro, which does the following:

  1. Load a folder full of images (Hundreds of pictures, but always a multiple of 3).
  2. Take the first 3 images.
  3. Of these 3 images take image 1, select a ROI, crop it, save the crop.
  4. Apply the same ROI for image 2 and image 3, crop and save the crops.
  5. Take the next three pictures, define a new ROI and repeat 3. to 4. until all the images are processed.

I came up with a macro which does all of this for every single image but I can´t figure out how to apply this cropping to bulks of 3 images, then the next 3, etc.

My images are usually named as follows:


Here is the code I got so far:

//Set default values
known_distance_in_mm = 0.001
known_distance_in_px = 4.4351
processed_dir_name = "MetaphasePositiveCropped"
processed_file_suffix = "_Crop.tif"
default_crop_width = 150
default_crop_height = 150

//Define directory to load from and set directory where cropped images should be saved
dir=getDirectory(“Choose a Directory”);
list = getFileList(dir);
processed_dir_name = dir + “MetaphasePositiveCropped”+File.separator;

//Batch processing
for (i=0; i<list.length; i++) {
print(i + ": " + dir+list[i]);

//open images
run(“Bio-Formats Importer”, “open=” + dir+list[i] + " color_mode=Default view=Hyperstack stack_order=XYCZT");

//Get image name
baseNameEnd=indexOf(imgName, “.tif”);
baseName=substring(imgName, 0, baseNameEnd);

//create selection, wait until selection is moved to my position and crop
if (nImages == i)
run(“Set Scale…”, “distance=known_distance_in_px known=known_distance_in_mm pixel=1 unit=mm”);
run(“Specify…”, “width=”+default_crop_width+" height="+default_crop_height+" x=0 y=0");

//select cropped image and save
saveAs(“Tiff”, processed_dir_name+baseName+processed_file_suffix);

I´d be happy for any kind of suggestions! Thanks in advance!

Cheers ,
Seb :sun_with_face:

Batch Processing without opening image

Hi Seb,

I created a workflow in using KNIME Image Processing, which I think solves your problem very well (see attachment). It allows you to specify the regions to crop in one go and they are then applied to all images in each group in bulk. If you have any further questions about the workflow or KNIME in general feel free to ask them!

BulkProcessingExample.knwf (29.3 KB)



Hi Gabriel,

this works great! Thanks a lot!

Seb :sun_with_face:



I am wondering if/how it is possible to crop multiple ROIs instead of just one. I tried to run your pipeline with having two ROIs but as a result I only got one cropped ROI and not both. What does the trick?

Thanks and cheers,

Seb :sun_with_face:


Yes this is easily possible, you need to change the settings for two nodes though:

  1. In the Interactive Annotator node, go to the Label Settings tab and click the “add unique segement id as label” checkbox. Now each created segment gets an unique label.
  2. To prevent the cropping of duplicate ROIs we now have to filter the labelings in the Segment Cropper node:
    1. Open the Cropping and Postprocessing meta-node,
    2. Configure the Segment Cropper node, go to the Segment Label Filter tab and add a rule with the text: “Segment:*” (this is the naming pattern of the unique labels).

See the attached an updated workflow where I already performed these steps.

BulkProcessingExample_v2.knwf (30.6 KB)



Awesome, thank you!