dm-script

Changing scale bar unit of length


I'm using the example script found from the F1 Help menu, "Set all scale-bars to same style" to insert a scale bar onto my ImageDisplay. The issue is that the scale bar's label defaults to micrometers, when I would prefer it to be in nanometers. Is there a way to use DM-script to change the scale bar's unit of length?

Side question: I see that in the example script, the line for initializing a new scale bar component is:

scalebar = NewComponent( kSCALEBAR, 0, 0, 1, 1 )

What are the 4 number-type arguments used for? I assume top, left, bottom, right, but in the F1 Help documentation it labels the parameters "f1, f2, f3, f4."


Solution

  • In order for the scale bar to display in nanometers, one needs to make sure the image is calibrated in nanometers. This is another one of those things that is not as easy to do via script as one would hope because DM only knows calibration units as a string in the metadata, not as a physical property of an image dimension.

    The basic idea is to first establish what the current image calibration units are and to convert them to nanometers (if appropriate). This means your script code will need to parse/interpret the unit string of an image dimension and then decide how/whether to convert it to nanometers.

    A very simple implementation might look something like the following:

    ImageDocument targetDoc = GetFrontImageDocument();
    Image targetImage := targetDoc.ImageDocumentGetImage(0);
    String units = targetImage.ImageGetDimensionUnitString(0);
    if (units == "µm")
    {
        Number scale = targetImage.ImageGetDimensionScale(0);
        targetImage.ImageSetDimensionScale(0, 1000 * scale);
        targetImage.ImageSetDimensionScale(1, 1000 * scale);
        targetImage.ImageSetDimensionUnitString(0, "nm");
        targetImage.ImageSetDimensionUnitString(1, "nm");
    }
    

    NOTE: this sample script assumes that both image dimensions have the same scale and unit string and that only images calibrated in micrometers are to be recalibrated.

    As for your side question, yes the four Number arguments to the NewComponent function are interpreted as top, left, bottom, right in the case of a scale bar annotation, but they might have different meanings for some types of annotations.