How to set XYZ scaling (pixelsize) from a python script

Hi guys,

I know there exist the option to run the SetScale plugins also from within python but I was looking for the possibility to set the XYZ scale for a new image directly using the respective commands. I searched a while inside the ImagePlus etc. documentation but did not really find what I was looking for (maybe I overlooked it …).
Any suggestions are welcome.

Sebi

God day!

No idea about python but here is what the recoder functiionality returns for Java:

IJ.run(imp, "Properties...", "channels=1 slices=114 frames=1 unit=µm pixel_width=2 pixel_height=3 voxel_depth=1.5");

The constants are just for demo purposes.

HTH

Herbie

2 Likes

Hi Herbie,

thanks, this works as well fro Python. For now I can use this one, but I am sure there must be another way how to do this as well.

Cheers,

Sebi

For now I use the following code but I will keep searching … :-):

@staticmethod
def setproperties(imp,
                    scaleX=1.0,
                    scaleY=1.0,
                    scaleZ=1.0,
                    unit="micron",
                    sizeC=1,
                    sizeZ=1,
                    sizeT=1):

    # check if scaleZ has a valid value to call modify the properties
    if scaleZ is None:
        scaleZ = 1

    IJ.run(imp, "Properties...", "channels=" + str(sizeC)
            + " slices=" + str(sizeZ)
            + " frames=" + str(sizeT)
            + " unit=" + unit
            + " pixel_width=" + str(scaleX)
            + " pixel_height=" + str(scaleY)
            + " voxel_depth=" + str(scaleZ))

    return imp

Hi @sebi06,

Have you checked the javadoc? Answers are often there:

imp.getCalibration().pixelHeight = 0.1
imp.getCalibration().pixelHeight = 0.2
imp.getCalibration().pixelDepth = 0.3
imp.getCalibration().setUnit("um")

John

3 Likes

Hi John, you mean like this (in Java)?

units = "micron" 

newCal = Calibration() 
newCal.pixelWidth = xCal 
newCal.pixelHeight = yCal
newCal.pixelDepth = zCal  
newCal.setXUnit(units) 
newCal.setYUnit(units)
newCal.setZUnit(units)  
imp.setCalibration(newCal) 
1 Like

I folled the hint from @bogovicj and now it works like this in python:

def setscale(imp,
             scaleX=1.0,
             scaleY=1.0,
             scaleZ=1.0,
             unit="micron"):

    # check if scaleZ has a valid value to call modify the properties
    if scaleZ is None:
        scaleZ = 1.0

    newCal = Calibration()
    newCal.pixelWidth = scaleX
    newCal.pixelHeight = scaleY
    newCal.pixelDepth = scaleZ
    newCal.setXUnit(unit)
    newCal.setYUnit(unit)
    newCal.setZUnit(unit)
    imp.setCalibration(newCal)

    return imp
1 Like