Create and show RGB image
Python Script
This script creates 3 images in Python and utilizes a DM-script to create and show a color-overlay image. It is an example of how Hybrid scripts can be used to access functionality not (yet) available in the Python language directly. The script shows how image-labels can be used to address images across the two scripts.
Preview
'''
This script creates 3 images in Python and utilizes a DM-script
to create and show a color-overlay image. It is an example for
how Hybrid scripts can be used to access functionality not (yet)
available in the Python language directly. The script shows how
image-labels can be used to address images across the two scripts.
'''
import DigitalMicrograph as DM
import numpy as np
# Create 3D DM-Images from Python Numpy arrays
DMImg1 = DM.CreateImage( np.arange(50,150).reshape(10,10).copy() )
DMImg2 = DM.CreateImage( np.arange(150,50,-1).reshape(10,10).copy() )
DMImg3 = DM.CreateImage( np.random.choice(150,size=(10,10)).copy() )
# Optionally display the sub-images
show = False
if ( show ):
DMImg1.ShowImage()
DMImg2.ShowImage()
DMImg3.ShowImage()
# Build DM-script to create RGB image from these
# We can use the image-labels as reference.
DMScript = '// This is a DM script' + '\n'
DMScript += 'RGBImage col := RGB('
DMScript += DMImg1.GetLabel() + ','
DMScript += DMImg2.GetLabel() + ','
DMScript += DMImg3.GetLabel() + ')' + '\n'
DMScript += 'col.ShowImage()'
# Optionally show DM script in new window
showScript = False
if ( showScript ):
DM.ClearResults()
print( 'The following DM script will be executed:\n\n' )
print( DMScript, '\n\n' )
# Run the DM script
DM.ExecuteScriptString( DMScript )
# Always delete DMImage variables when no longer needed
# Note that they are needed during DM script execution
# as they ensure the images are kept in memory even if
# not displayed on the workspace.
del DMImg1
del DMImg2
del DMImg3