Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Calculating a 1-2-5 series #9

Open
aleskl opened this issue Dec 16, 2021 · 0 comments
Open

Calculating a 1-2-5 series #9

aleskl opened this issue Dec 16, 2021 · 0 comments

Comments

@aleskl
Copy link

aleskl commented Dec 16, 2021

Lukas, thank you for this excellent tool!

I recently did my own macro for automatic scalebars, but very rudimentary and not so user friendly :) I would just like to share with you a piece of code that calculates the scalebar length in nice numbers: it calculates a 1-2-5 series until it reaches 1/10th of the image, perhaps it would be useful.

Cheer, Ales

// Add scale bar to image in 1-2-5 series size

// set the appearance of scalebar
scalebarsettings = "height=10 font=24 color=White background=None location=[Lower Right] bold overlay"; 
scalebarsize = 0.1; // approximate size of the scale bar relative to image width

getPixelSize(unit,w,h);
if (unit == "pixels") exit("Image not spatially calibrated");

imagewidth = w*getWidth();  // image width in measurement units
scalebarlen = 1; // initial scale bar length in measurement units

// recursively calculate a 1-2-5 series until the length reaches scalebarsize, default to 1/10th of image width
// 1-2-5 series is calculated by repeated multiplication with 2.3, rounded to one significant digit
while (scalebarlen < imagewidth * scalebarsize) {
	scalebarlen = round((scalebarlen*2.3)/(Math.pow(10,(floor(Math.log10(abs(scalebarlen*2.3)))))))*(Math.pow(10,(floor(Math.log10(abs(scalebarlen*2.3))))));
}

run("Scale Bar...", "width=&scalebarlen "+scalebarsettings);

@lukmuk lukmuk mentioned this issue Sep 21, 2022
Merged
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant