Add text to lower-right of each image in a folder

Hi,

My intention is to add a text with specific formatting at the lower-right corner of multiple images of different sizes located in a folder. Is there any way of doing this automatically?

Thanks,

LH

@insertsk8

Yes! You’ll have to script something… a short macro that will do the same to each image, etc.

I used this template to develop this example macro script:

#@ File (label = "Input directory", style = "directory") input
#@ File (label = "Output directory", style = "directory") output
#@ String (label = "File suffix", value = ".tif") suffix
#@ String (label = "Text to add to images") text

processFolder(input);

// function to scan folders/subfolders/files to find files with correct suffix
function processFolder(input) {
	list = getFileList(input);
	list = Array.sort(list);
	for (i = 0; i < list.length; i++) {
		if(File.isDirectory(input + File.separator + list[i]))
			processFolder(input + File.separator + list[i]);
		if(endsWith(list[i], suffix))
			processFile(input, output, list[i], text);
	}
}

function processFile(input, output, file, text) {
	// open one image
	run("Bio-Formats", "open=[" + input + "/" + file +"] autoscale color_mode=Default rois_import=[ROI manager] view=Hyperstack stack_order=XYCZT");
  	originalTitle = getTitle();
  	getDimensions(width, height, channels, slices, frames);
  	stringWidth = getStringWidth(text);
  	drawString(text, width-stringWidth-10, height);
  	// adapt more if needed...
}

You can do even more with changing the fonts/colors/etc… Just search for those text functions in the Built In Macro Functions list. I used the drawString() function:

drawString(“text”, x, y)
Draws text at the specified location. The first character is drawn obove and to the right of (x,y). Call setFont() to specify the font. Call setJustification() to have the text centered or right justified. Call getStringWidth() to get the width of the text in pixels. Refer to the TextDemo macro for examples and to DrawTextWithBackground to see how to draw text with a background.

There are other demos and example macros available there to help you. But to also give you some info on scripting in ImageJ/Fiji - here are some helpful links:

1 Like

I used this macro in Process > Batch > Macro and it works fine except for the font size.
I’ve got images of different sizes/resolutions in my folder and the font size I use is fixed so many low-resolution images have much larger texts than the rest. Is there any function I could use to solve this issue?

text = "This is the image text";
getDimensions(width, height, channels, slices, frames);
setFont("Calibri", 76, "bold, antialised, white");
stringWidth = getStringWidth(text);
drawString(text, width-stringWidth-10, height);

Thanks again!

LH

@insertsk8

I suppose you could use an if/else statement - and if the size of the image is above a certain number - set the font to be larger… you will have to play a bit with the code… you could even set ranges by chaining the if/else statements - so large image (>##) = large font, medium image (##-#) = med font, and small image (<#) = small font.

Ok, I’ll try that!
Replacing the font size number (i.e. 76) to "height/ also works.

Thanks a million.

Liam

1 Like