0

How do I add a text annotation with python in (Edit ... in Version 3.4.0)?


I want to add some text to images by using python in the GMS python environment. Therefore I want to use text annotations.

I can create a text annotation by using DM.NewTextAnnotation(). But the returned DM.Py_Component object does not have any ComponentAddChild...() methods. So I can create text annotations but I cannot add them.

There also is a DM.Py_Component.AddNewComponent(type, f1, f2, f3, f4) method. I can create text annotations with that (using type = 13). But I can only specify the position with the parameters f1 to f4. Using a string argument raises a TypeError. There is a DM.Py_Component.GetText() and several font manipulation methods but no DM.Py_Component.SetText(). So I can create text annotations which are already appended to the parent component but without text. And I cannot set the text.

The dm-script docs also talk about a Component::ComponentExternalizeProperties() which lets me assume that there is a TagGroup in the background of each component. Is there any way to manipulate that, even though there is no DM.Py_Component.ExternalizeProperties() in the python module.


So my question is: What is the intended way of adding text annotations to images? Is there any way of either adding annotations to components or setting the text of an added annotation?

2
  • 1
    Yes, these commands were missing in initial releases of the Python API and are essential. Please upgrade to the latest GMS version. Commented Jan 19, 2021 at 11:17
  • 1
    I've added a hybrid-script example to my answer in case you cannot upgrade. Commented Jan 19, 2021 at 11:35

1 Answer 1

0

The mentioned missing commands have been added with the most recent release GMS 3.4.3. Without them, there is no way of adding components except with some creative hybrid-coding.

With the commands, the correct example is:

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

and for changing text of an existing text component (type 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


If you need to do this with a version prior GMS 3.4.3 you can work around the missing command by calling a DM script from your Python script as in this example:

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
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.