Fiji change calibration without doing the math

Dear Forum,
I am trying to change the calibration of an image via Jython.
I have some images in nm, some in um and I want all of them in um.

Is there an elegant way to change it programmatically?
Without considering what is the actual calibration of the image.

I see that using the Menu-Image-Properties it works.
If I change to um, Fiji directly converts for me the pixel width and length.
I am failing to find this same thing programmatically.

Thank you,
have a nice day


The Analyze/Set Scale window has an option’Global’ … for all images

As macro command it looks like

run("Set Scale...", "distance=5 known=1 pixel=1 unit=mm global");

Hi @emartini!

Unfortunately, the methods that are called to perform that conversion on the fly (they occur as you type in the dialog units field) are private, written mostly for the dialog function. See here for the most relevant parts of the code:

Nonetheless, you can probably get away with this makeshift implementation:

from ij import WindowManager as wm

img = wm.getFrontWindow().getImagePlus()
cal = img.getCalibration()

if cal.getXUnit() == "nm":
	cal.pixelWidth = cal.pixelWidth/1000.0
	cal.pixelHeight = cal.pixelHeight/1000.0
	cal.pixelDepth = cal.pixelDepth/1000.0


1 Like

Hi Nico,
yes this is how I am approaching the problem at the moment.
that private method is very nice (main reason it would check for me other unit conversions), I hoped that somewhere was possible to get it with a public method.

Yes, I totally agree. It would nice if this was part of Calibration instead (would it be worth the effort to implement it that way?). The main problem here is not that it is private (you can get around that using reflection, although I’m not sure how much more code overhead that would mean in this particular case), but, that in the end, you’ll still have to do the steps inside the if statement anyway to actually set the results.


1 Like