I’m working with Gatan DigitalMicrograph (DM-script) and trying to programmatically access the label text shown on a scale bar, such as "200 nm"
or "10 μm"
.
I assumed this information would be stored in the tag group associated with the scale bar, so I tried the following:
Image img := GetFrontImage()
ImageDisplay img_disp = img.ImageGetImageDisplay(0)
Component scale = img_disp.ComponentAddNewComponent(31, 0, 0, 1, 1)
scale.ComponentSetRect(200, 200, 250, 300)
TagGroup tg = scale.ComponentGetTagGroup()
tg.TagGroupOpenBrowserWindow(0)
However, the tag group appears to be empty.
How can I retrieve the scale bar label text (such as "500 nm"
) from a DigitalMicrograph image using DM-script?
Is this value stored somewhere accessible, or is it generated only during rendering?
The short answer to your question is yes, the actual scale bar label is generated only during rendering and is never recorded in any of the properties or TagGroup data of the scale bar component.
However, although a bit complicated, one can easily, in a script, reproduce the computation DM does to establish the scale bar length from the width of its enclosing rectangle, as follows:
Image frontImage := GetFrontImage();
ImageDisplay frontDisplay = frontImage.ImageGetImageDisplay(0);
for (Number iComp = 0; iComp < frontDisplay.ComponentCountChildren(); iComp++)
{
Number scaleBarType = 31;
Component nextComp = frontDisplay.ComponentGetChild(iComp);
if (nextComp.ComponentGetType() != scaleBarType)
continue;
Number scaleBarT, scaleBarL, scaleBarB, scaleBarR;
nextComp.ComponentGetRect(scaleBarT, scaleBarL, scaleBarB, scaleBarR);
Number xScale = frontImage.ImageGetDimensionScale(0);
String xUnits = frontImage.ImageGetDimensionUnitString(0);
// Establish the scale bar power of 10
Number scaleBarBoxW = (scaleBarR - scaleBarL) * xScale;
Number logScaleBarBoxW = scaleBarBoxW.Log10();
Number scaleBarPowerOf10 = logScaleBarBoxW.Floor();
// Establish the scale bar factor: 1, 2, or 5
Number logScaleBarFactor = logScaleBarBoxW - scaleBarPowerOf10;
Number scaleBarFactor = 5;
if (logScaleBarFactor < log10(2))
scaleBarFactor = 1;
else if (logScaleBarFactor < log10(5))
scaleBarFactor = 2;
// Compute the scale bar length
Number scaleBarLength = scaleBarFactor * scaleBarPowerOf10.Exp10();
OKDialog("Scale bar length: " + scaleBarLength + " " + xUnits);
}