Set scale automatically from text file

Hi everyone,

is it possible to automatically calibrate an image with the pixel size information provided in a text file?
I have a large set of SEM images that are unfortunately not calibrated. The pixel size information is, amongst other info, saved in a text file with the same name (each image has its own text file). Right now, I’m opening the text file, copy the pixel size manually, and paste it into “Analyze >> Set Scale…”.
Unfortunately, my programming knowledge is nearly non-existent.

Hi @Aveni,

I had to do something similar once, maybe it can work for your situation if it’s adapted a little? The relevant segment of the macro I used is below. To be honest I always assumed there would be a more elegant way of doing it (and maybe someone will contribute that!) but never went back to check cause it worked for what I needed at the time.

It opens a text file as a string and goes through the lines until it finds one that contains “Pixel size”. It then splits (using “:” and “x”) that line up so that one of the resultant parts (specifically the fourth one) is the value needed to be put in the “Set Scale…” line.

So it assumes that the text file that is going to be opened has multiple lines and that one reads like below:
“Pixel size: 0.3250x0.3250 micron”

Your file might not be in that configuration which is why I think you’d need to adapt it. You’d also be able to change the first two lines so that the macro looks for a text file with the same name as the open image file instead of having to pick the file each time.

filePath = File.openDialog("Choose the text file to open:"); 
fileString = File.openAsString(filePath); 

rows = split(fileString, "\n"); 
for (i = 0; i < rows.length-1; i++){
	check = indexOf(rows[i], "Pixel size");

	if (check == -1){
	} else { 
		arrayIndex = i;

relevantLine = rows[arrayIndex];
scaleValueParts = split(relevantLine, "x|:| ");

scaleValue = scaleValueParts[3];

run("Set Scale...", "distance=1 known=" + scaleValue + " unit=micron");

Hope this is helpful, feel free to ask more about anything if it’s not clear.

1 Like