简体   繁体   English

如何通过脚本打开带有注释的图像

[英]How to open an image with annotations by script

How do I open an (dm4) image with annotations in a script in dm-script?如何在 dm-script 的脚本中打开带有注释的 (dm4) 图像?


When a dm4 image has annotations (eg a scale bar or some text), this is displayed when I open the image via the menu (Ctrl + O).当 dm4 图像具有注释(例如比例尺或某些文本)时,当我通过菜单 (Ctrl + O) 打开图像时会显示。 But when I open the same file in a script by openImage() they do not show up as shown below.但是当我通过openImage()在脚本中打开同一个文件时,它们不会如下所示显示。

On the left there is the image opened via the menu, on the right is the exact same image opened by openImage() .左边是通过菜单打开的图像,右边是通过openImage()打开的完全相同的图像。 It is missing the annotations.它缺少注释。

真实形象 通过脚本打开的图像

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();

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).它的出现是因为脚本旨在启用非常简单的脚本,例如Z = log(A) ,其中通过处理现有图像(此处为 A)即时创建新图像(此处为 Z)。

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:有关详细信息,请参阅此处的 F1 帮助文档: 在此处输入图像描述

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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM