【问题标题】:How to add text annotations with python in dm-script如何在 dm-script 中使用 python 添加文本注释
【发布时间】:2021-04-23 14:02:13
【问题描述】:

如何在中使用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()


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

【问题讨论】:

  • 是的,这些命令在 Python API 的初始版本中是缺失的,并且是必不可少的。请升级到最新的 GMS 版本。
  • 我在答案中添加了一个混合脚本示例,以防您无法升级。

标签: dm-script python annotations dm-script


【解决方案1】:

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

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

testImg = DM.GetFrontImage()
img_disp = testImg.GetImageDisplay(0)
textComp = DM.NewTextAnnotation(0, 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, 0, "'
dmScript += annotext 
dmScript += '", 15)' + '\n'
dmScript += 'disp.ComponentAddChildAtEnd( anno )' + '\n'
#print( dmScript )

# Run the DM script
DM.ExecuteScriptString( dmScript )

# Cleanup 
del testImg

【讨论】:

    猜你喜欢
    • 2023-01-11
    • 1970-01-01
    • 1970-01-01
    • 2023-01-09
    • 2015-09-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多