///////////////////////////////////////////////////////////////////// // (c) Gatan Inc. ///////////////////////////////////////////////////////////////////// // This example demonstrates how to annotate images using image document components // The script annotates the front-most image, illustrating how to format, position, // search for text annotations and how to extract information from them. ///////////////////////////////////////////////////////////////////// // last modified 08-July-2014 BS // Define the font size and style to be used string fontName = "Arial" number fontSize = 10 number true =1, false = 0 /* This method adds text annotation to an existing image document component. It is called with an existing image document component, column and row co-ordinates, the annotation text and a name for the annotation. The annotation name is added to the components tag group for reference. The method returns the new annotation image document component. */ component AddTextAnnotation( Component comp, Number col, Number row, String text, string name ) { // Initialise annotation co-ordinate variables number top, left, bottom, right, maxBottom = 0 // Find how many annotations are present in component 'comp', and loop over each number i, count = comp.ComponentCountChildren() for (i=0; i < count; i++) { // Find last annotation in this group (last in y position, that is) Component compChild = comp.ComponentGetChild( i ) // Finding the bottom most co-ordinate of the i'th annotations restangular bounds... ComponentGetRect( compChild, top, left, bottom, right ) // ... and set as 'maxbottom' if bottom-most if (bottom > maxBottom) maxBottom = bottom } // Declare a new component called 'annot' component annot // Create new text annotation component. Note this fontSize doesn't work annot = NewTextAnnotation( 0, maxBottom, text, fontSize ) // Add the child component 'annot' to the end of the parent 'comp's list of sub-annotations. comp.ComponentAddChildAtEnd( annot ) // Set the font size and style. Must be called AFTER adding component to the image display. annot.componentSetFontSize( fontSize ) annot.ComponentSetFontFaceName( fontName ) // Open an OK dialog containing the current font style okdialog( annot.ComponentGetFontFaceName( ) ) // Get the component tag group into a tag group 'tgs', and place // the annotation name string into the tag group, under the entry 'name' TagGroup tgs = annot.ComponentGetTagGroup() tgs.TagGroupSetTagAsString( "name", name ) // Return the new annotation component return annot } /* This method adds a new group annotation component to an image at specified co-ordinates It is called with the image to be annotated, and the annotaion box co-ordinates. The method returns the new image document component */ component AddGroupAnnotation(Image img, Number top, number left, number bottom, number right ) { // Get the height (y-dimension) size of the input image in pixels number height = img.ImageGetDimensionsize( 1 ) // Create a new group annotation component, andet the rectangular bounds of the annotation component annot = NewGroupAnnotation( ) annot.ComponentSetRect( top, left, bottom, right ) // Add the child component 'annot' to the end of the image display document of 'img' // and return the image document component 'annot' img.ImageGetImageDisplay(0).ComponentAddChildAtEnd(annot) return annot } /* This method searches through an input image document component for annotaion of the specified name, as written to the tag group in the AddTextAnnotation method The method is called with the parent image document component and a name string of the annoation It returns the annotaion child component. */ Component FindAnnotationWithName( Component comp, string compname ) { // Create the output component, and initialise the flag 'found' to zero Component desiredComp number found = false // Count the number of child components (annotations) associated with the parent component comp // and loop through all the child components while the desired component isn't found number i, count = comp.ComponentCountChildren() for (i=0; i