miile7
miile7

Reputation: 2393

How to open an image with annotations by script

How do I open an (dm4) image with annotations in a script in dm-script?


When a dm4 image has annotations (e.g. a scale bar or some text), this is displayed when I open the image via the menu (Ctrl + O). But when I open the same file in a script by openImage() they do not show up as shown below.

On the left there is the image opened via the menu, on the right is the exact same image opened by openImage(). It is missing the annotations.

Real image Image opened via script

The following example shows the same thing. The code adds text to an image, saves it and opens it again. The opened image does not show the annotations just as the images above:

String path = GetApplicationDirectory("current", 0);
path = PathConcatenate(path, "temp.dm4");

// get the current image
image img;
img.getFrontImage();
ImageDisplay display = img.ImageGetImageDisplay(0);

// add some test annotations
number height = img.ImageGetDimensionSize(1);
number padding = height / 100;
number font_size = height/10;
for(number y = padding; y + font_size + padding < height; y += font_size + padding){
    Component annotation = NewTextAnnotation(padding, y, "Test", font_size);
    annotation.componentSetForegroundColor(255, 255, 255);
    display.ComponentAddChildAtEnd(annotation);
}

// save the current image
img.saveImage(path);

// show the saved image
image img2 = openImage(path);
img2.showImage();

Upvotes: 0

Views: 250

Answers (1)

BmyGuest
BmyGuest

Reputation: 2939

You have a mistake in the second to last line. By using = instead of := you are copying (the values only) from the opened image into a new image. You want to do

image img2 := openImage(path)

This is a rather typical mistake made when being new to scripting, because this is a "specialty" of the scripting language not found in other languages. It comes about because scripting aims to enable very simple scripts like Z = log(A) where new images (here Z) are created on-the-fly from processing existing images (here A).

So there needs to be a different operator when one wants to assign an image to a variable.

For further details, see the F1 help documentation here: enter image description here

The same logic / source of bugs concerns the use of := instead of = when "finding" images, "creating new images" and cloning images (with meta data). Note the differences when trying both:

image a := RealImage("Test",4,100,100)
ShowImage(a)

image b = RealImage("Test",4,100,100)
ShowImage(b)

and

image a := GetFrontImage()
a = 0

image b = GetFrontImage()
b = 0

and

image src := GetFrontImage()
image a := ImageClone( src )
showImage(a)

image b := ImageClone( src )
showImage(b)

Upvotes: 0

Related Questions