如何解决如何在 dm-script
我有一个记录图像的应用程序。我想为每个记录的图像添加一个比例尺。但用户可以选择不显示录制的图像。这就是为什么我想在显示图像之前并独立于显示图像添加比例尺。
文档建议使用 ImageDocumentSavetoFile()
进行保存。因此,我正在为图像创建一个图像文档。由于比例尺添加到图像显示中,我还添加了图像显示(如果没有给出显示)。但是当我保存图像文档并再次加载时,出现以下错误:
这告诉我在某处我将图像两次添加到图像文档中。但我不知道在哪里(下面发布的代码)。
我可以从保存到的路径手动打开图像。图像显示了内容(此处为纯色),但未显示比例尺(如上所示的屏幕截图)。所以我想我以某种方式添加了两个显示或图像,其中一个包含比例尺,另一个不包含。但是我现在不知道怎么办,怎么解决这个问题。
我的精简代码发布在下面。
所以我的问题是:我的代码中是什么导致比例尺不显示以及是什么导致 GMS 在打开图像时引发错误?或者更好的是,在不显示图像的情况下向图像添加注释的最佳做法是什么?
number image_width = 128;
number image_height = 128;
image img := RealImage("Image",4,image_width,image_height);
img = 128;
ImageDocument doc = img.ImageGetorCreateImageDocument();
void addScalebar(Imagedisplay display){
number top = image_height - image_height / 5;
number bottom = image_height;
number left = 0;
number right = image_width / 3;
Component scalebar = NewComponent(31,top,left,bottom,right);
scalebar.ComponentSetForegroundColor(0,255,0);
display.ComponentAddChildAtBeginning(scalebar);
}
image tmp_img;
for(number i = 0; i < doc.ImageDocumentCountimages(); i++){
tmp_img = doc.ImageDocumentGetimage(i);
if(tmp_img.ImageCountimagedisplays() > 0){
for(number j = 0; j < tmp_img.ImageCountimagedisplays(); j++){
addScalebar(tmp_img.ImageGetimagedisplay(j));
}
}
else{
Imagedisplay disp = doc.ImageDocumentAddImagedisplay(tmp_img,-2);
addScalebar(disp);
}
}
string save_format = "Gatan Format";
string path = PathConcatenate(GetApplicationDirectory("temporary",1),"demo-image");
doc.ImageDocumentSavetoFile(save_format,path);
string open_path;
ImageDocumentAdjustFileNameForSaveFormat(save_format,path,open_path);
result("Demo image is located at '" + open_path + "'\n");
image display_image := OpenImage(open_path);
display_image.ShowImage();
贴出的代码是用dm-script
写的。我的原始代码是用 python 编写的,这是对 dm-script
的 more-less 直接翻译,对于 dm-script 可能更容易理解。另外,我觉得在 dm-script
中发布问题更“笼统”。该问题适用于两种编程语言,我希望该解决方案也适用。如果没有,如果你知道 python 实现,我也很高兴看到它。
解决方法
非常好的剧本。 您陷入了典型的DM 初学者陷阱,不幸的是,脚本语言的语法非常不标准。
两者之间有区别:
tmp_img = doc.ImageDocumentGetImage(i);
和:
tmp_img := doc.ImageDocumentGetImage(i);
第二个例子是你想要做的:让图像变量 tmp_img
指向 imageDocument 的第 i 个图像。
然而,第一个示例将第 i 个图像的数据值复制到新创建的图像中。这个新创建的图像没有 imageDisplay!这就是您的代码示例分支到“else”语句的原因。
另请注意,如果您事先修复了线路,则不需要 if/else。 ImageDocuments 不能保存图像,它们保存 imageDisplays(与图像相关联)。 更具体地说:ImageDocuments 有一个根组件,与所有组件一样,它可以有子组件。 ImageDisplays 也是组件。
,这是问题的 Python 变体的答案。 以下应该可以做你想做的事(在 Python 中):
import numpy as np
imgArray = np.arange(200000).reshape(400,500).copy(order='C')
testImg = DM.CreateImage(imgArray)
doc = testImg.GetOrCreateImageDocument()
doc.GetRootComponent().GetNthChildOfType(20,0).AddNewComponent(31,10,40,400)
path = 'C:/temp/uniqueName3.dm4'
doc.SaveToFile('Gatan Format',path)
# Cleanup
del testImg
del doc
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。