Scripting Resources for DigitalMicrograph™

banner

Dave Mitchell's DigitalMicrograph™ Scripting Website

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

 

Image Downsampler
Function
Downsamples the frontmost image to create a smaller version. Use this for creating source images for use with the 'Thumbnail Creator' script (creates hard coded image files).
Version
version:20090815. v1.0
Author
D. R. G. Mitchell
Acknowledgements
Comments
System Requirements
Should be compatible with all recent versions of DigitalMicrograph.
Known Issues
Supported
Yes
Included Files
Main script file.
Source Code

// Script to create a downsampled image (thumbnail) from the passed in image

// D. R. G. Mitchell, adminnospam@dmscripting.com (remove the nospam to make this address work)

// Aug. 2009, v1.0

// version:20090815

 

image createthumbnail (image bigimage, number thumbx, number thumby)

{

number imagexsize, imageysize

getsize(bigimage, imagexsize, imageysize)

 

image thumbnail=integerimage("",2,1,thumbx, thumby)

 

 

// Only downsample if the source image is larger than the target size.

 

if(imagexsize>=thumbx && imageysize>=thumby)

{

number xscale=trunc(imagexsize/thumbx)

number yscale=trunc(imageysize/thumby)

thumbnail=bigimage[icol*xscale, irow*yscale]

}

else

{

showalert("Thumbnail size is greater than source image size!\nSource Image : X = "+imagexsize+" "+"Y = "+imageysize+" pixels.",2)

exit(0)

}

 

return thumbnail

 

}

 

 

// Main program starts here

 

// Source the frontmost image

 

image front:=getfrontimage()

 

// Get the image size

 

number thumbx, thumby, sizex, sizey

getsize(front, sizex, sizey)

 

// Ask for the thumbnail sizes in x and y. The ysize is scaled to

// have the same relative donwsample as the xsize

 

if(!getnumber("Enter the thumbnail X size (pixels)",32, thumbx)) exit(0)

number reducedy=round(sizey/(sizex/thumbx))

if(!getnumber("Enter the thumbnail Y size (pixels)",reducedy, thumby)) exit(0)

 

// Minimum size is 10 pixels

 

if(thumbx<10) thumbx=10

if(thumby<10) thumby=10

image thumbnail=createthumbnail(front, thumbx, thumby)

 

showimage (thumbnail)