I'm working in Gatan DigitalMicrograph (DM-script), and I want to create an image window (ImageDocument) that I can reuse to display different images over time. The images may have different dimensions (e.g., 256×256, 512×512), and I would like to update the display content dynamically based on need.
Currently, my approach is to create an ImageDocument, add multiple images to it, and then switch between them using ImageDocumentSwitchToImageMode(...). Here is a simplified version of my current script:
// Create the ImageDocument
ImageDocument imageDoc = CreateImageDocument("New ImageDocument")
// Create a 256x256 image
number width = 256, height = 256
Image img := RealImage("dummy", 4, width, height)
img = irow
// Add to the document
imageDoc.ImageDocumentAddImage(img)
// Create a second 512x512 image
Image img2 := RealImage("dummy2", 4, 512, 512)
img2 = icol
// Add to the document as well
imageDoc.ImageDocumentAddImage(img2)
// Get their display objects
ImageDisplay imgDisplay = img.ImageGetImageDisplay(0)
ImageDisplay imgDisplay2 = img2.ImageGetImageDisplay(0)
// Show and switch
imageDoc.ImageDocumentShow()
Sleep(1)
imageDoc.ImageDocumentSwitchToImageMode(imgDisplay2)
This works, but it feels like a workaround.
Question: Is there a cleaner or more elegant way in DM-script to:
Any advice or example scripts would be greatly appreciated.
First of all, you own solution isn't bad. It could be improved upon by ensuring you remove the no-longer-needed imageDisplay from the document, so that the document only has a single image ever:
image img1 := RealImage("Test 512 x 128",4,512,128)
img1.ImageGetTagGroup().TagGroupSetTagAsString("Info","I'm image A")
image img2 := RealImage("Test 128 x 512",4,128,512)
img2.ImageGetTagGroup().TagGroupSetTagAsString("Info","I'm image B")
img1.ShowImage()
OKDialog("Now Swap me")
imagedocument doc =img1.ImageGetOrCreateImageDocument()
doc.ImageDocumentAddImage(img2)
doc.ImageDocumentSwitchToImageMode(img2.ImageGetImageDisplay(0))
img1.ImageGetImageDisplay(0).ComponentRemoveFromParent()
It could be further improved upon by keeping the overall window the same, if you want that:
image img1 := RealImage("Test 512 x 128",4,512,128)
img1.ImageGetTagGroup().TagGroupSetTagAsString("Info","I'm image A")
image img2 := RealImage("Test 128 x 512",4,128,512)
img2.ImageGetTagGroup().TagGroupSetTagAsString("Info","I'm image B")
img1.ShowImage()
OKDialog("Now Swap me")
imagedocument doc =img1.ImageGetOrCreateImageDocument()
number wt,wl,wb,wr
doc.ImageDocumentGetWindow().WindowGetFrameBounds(wt,wl,wb,wr)
doc.ImageDocumentAddImage(img2)
doc.ImageDocumentSwitchToImageMode(img2.ImageGetImageDisplay(0))
img1.ImageGetImageDisplay(0).ComponentRemoveFromParent()
doc.ImageDocumentGetWindow().WindowSetFrameBounds(wt,wl,wb,wr)
An alternative is the command that switches (just) the data section of an image.
Note, however, that this will keep the meta data from the original image. Sometimes this is what one wants. Sometimes one wants to copy tags and other stuff over afterwards.
image img1 := RealImage("Test 512 x 128",4,512,128)
img1.ImageGetTagGroup().TagGroupSetTagAsString("Info","I'm image A")
image img2 := RealImage("Test 128 x 512",4,128,512)
img2.ImageGetTagGroup().TagGroupSetTagAsString("Info","I'm image B")
img1.ShowImage() OKDialog("Now Swap me")
img1.ImageSwapData(img2) // Meta-Data is still of img1