Scripting Resources for DigitalMicrograph™


Dave Mitchell's DigitalMicrograph™ Scripting Website

Home | Scripts | Examples | Functions | Recent Updates | Tutorials | Resources | Publications | Consulting | Projects | Contact & Bio | Private | Search


Invert Image Contrast
Inverts the contrast of the front-most image. In binary-valued images, 0 become 1 and vice versa. In greyscale images the minimum becomes the maximum and vice versa, with everything in between scaled appropriately.
version:20160325, v2.0
D. R. G. Mitchell
This script works on 1D and 2D images.
System Requirements
Should be compatible with all recent versions of DigitalMicrograph.
Known Issues
Included Files
Main script file.
Source Code


// Script to invert the front-most image's contrast. An image which has binary values

// will be modified so that 0 become 1 and vice versa. For greyscale images

// where the values might run from say 10 to 232, the 232 value become 10 and vice

// versa, with all values in between inverted/scaled appropriately. The image and

// its inverse have the property that when summed the entire image has

// a constant value .



// D. R. G. Mitchell, (remove the nospam to make this work)

// version 20160325, v2.0, Mar 2016,

// Check an image is displayednumber nodocs=countdocumentwindowsoftype(5)




showalert("Ensure an image is displayed front-most.",2)



// Source the front-most image and get some info from it


number minval, maxval, range, xsize, ysize

image front:=getfrontimage()

front.minmax(minval, maxval)

front.getsize(xsize, ysize)

string imgname=getname(front)




// Create an ouputimage. This must be real so that it can handle the inversion maths below. If it were

// integer then a blank image would result from inversion, since the image is normalised values between

// zero and one during the conversion.


// Create a copy of front (real by default) and copy the calibrations and taggroups from the front image to it.


image output=front

imagecopycalibrationfrom(output, front)

taggroup fronttags=front.imagegettaggroup()

taggroup outputtags=output.imagegettaggroup()

taggroupcopytagsfrom(outputtags, fronttags)


// Normalise the values to run from 0 to 1




// Invert the values so that zero is 1 and vice versa





// Create the original value range




setname(output, imgname+" (inverted)")


// set the image display to autosurvey. Different commands are needed

// for intensity profiles (ysize=1) and images


imagedisplay outputdisp=output.imagegetimagedisplay(0)

if(ysize>1) outputdisp.imagedisplaysetdoautosurvey(1)

if(ysize==1) outputdisp.lineplotimagedisplaysetdoautosurvey(1,1)