微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何在 dm-script 中使用 python 添加文本注释

如何解决如何在 dm-script 中使用 python 添加文本注释

如何在 中使用 python 添加文本注释(编辑 ...在 3.4.0 版中)?


我想通过在 GMS python 环境中使用 python 为图像添加一些文本。因此我想使用文本注释。

我可以使用 DM.NewTextAnnotation() 创建文本注释。但是返回的 DM.Py_Component 对象没有任何 ComponentAddChild...() 方法。所以我可以创建文本注释,但我不能添加它们。

还有一个 DM.Py_Component.AddNewComponent(type,f1,f2,f3,f4) 方法。我可以用它创建文本注释(使用 type = 13)。但我只能用参数 f1f4 指定位置。使用字符串参数会引发 TypeError。有 DM.Py_Component.GetText() 和几种字体操作方法,但没有 DM.Py_Component.SetText()。所以我可以创建已经附加到父组件但没有文本的文本注释。而且我无法设置文本。

dm-script 文档还谈到了 Component::ComponentExternalizeProperties(),这让我假设每个组件的背景中都有一个 TagGroup。即使 python 模块中没有 DM.Py_Component.ExternalizeProperties(),有什么方法可以操作它。


所以我的问题是:向图像添加文本注释的预期方式是什么?有没有办法给组件添加注解或设置添加注解的文本?

解决方法

上述缺失的命令已在最新版本 GMS 3.4.3 中添加。 没有它们,除了一些创造性的混合编码之外,没有办法添加组件。

使用命令,正确的例子是:

testImg = DM.GetFrontImage()
img_disp = testImg.GetImageDisplay(0)
textComp = DM.NewTextAnnotation(0,'test new text annotation',15)  
img_disp.AddChildAtEnd(textComp)

# Cleanup 
del img_disp
del testImg

以及更改现有文本组件的文本(类型 13):

testImg = DM.GetFrontImage()
img_disp = testImg.GetImageDisplay(0)

nSubComp = img_disp.CountChildren()
for index in range(nSubComp):
    comp = img_disp.GetChild(index)
    if ( comp.GetType() == 13 ):
        comp.TextAnnotationSetText( 'Other text' )

# Cleanup 
del img_disp
del testImg


如果您需要使用 GMS 3.4.3 之前的版本执行此操作,您可以通过从 Python 脚本调用 DM 脚本来解决缺少的命令,如下例所示:

annotext = 'This is the annotation'
testImg = DM.GetFrontImage()

# Build a DM script as proxy
dmScript = '// This is a DM script' + '\n'
dmScript += 'imageDisplay disp = ' + testImg.GetLabel() + '.ImageGetImageDisplay(0)' + '\n'
dmScript += 'component anno = NewTextAnnotation( 0,"'
dmScript += annotext 
dmScript += '",15)' + '\n'
dmScript += 'disp.ComponentAddChildAtEnd( anno )' + '\n'
#print( dmScript )

# Run the DM script
DM.ExecuteScriptString( dmScript )

# Cleanup 
del testImg

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。