Skip to content

Instantly share code, notes, and snippets.

@frederickding
Created July 27, 2012 19:02
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save frederickding/3189844 to your computer and use it in GitHub Desktop.
Save frederickding/3189844 to your computer and use it in GitHub Desktop.
ImageJ macro to convert DM3 digital micrograph files (e.g. transmission electron microscope) to TIF and PNG
/**
* ImageJ macro to convert DM3 digital micrograph files to TIF and PNG
*
* @author Frederick Ding
*/
/*
* Option switches
*
* debug: when set to true, some variables will be outputted to the log
*/
debug = false;
/*
* Get some information about the pixel/image dimensions
*
* Pixels do not equal physical size; this tells us how much physical size
* each pixel is, such as 0.005 nm or 0.005 microns.
*/
getPixelSize(unit, pw, ph);
if(debug) {
print(pw);
print(unit);
}
/*
* Calculate the width of a bar in the most relevant physical unit
*
* ImageJ uses the scaleWidth in whatever units make most sense to it given the
* physical dimensions of the image, but we can approximate one of a minimum
* pixel width of 200 (on the original picture).
*/
scaleWidth = floor(256.0 * pw / 10.0) * 10.0;
while(scaleWidth / pw < 200) {
if(unit == "nm") {
scaleWidth += 5;
} else if(unit == "microns") {
scaleWidth += 0.5;
}
}
if(debug) {
print(scaleWidth);
}
/* scaleWidth = getNumber("Width of scale bar (automatically in nm or µm)", 5); */
/* Calculate an output directory in a subfolder called 'out2' relative to the original image */
filePath = getDirectory("image") + "out2" + File.separator + getInfo("image.filename");
/*
* Create a scaled down 640x640, 8-bit PNG file with a scale bar.
*/
run("Scale...", "x=- y=- width=640 height=640 interpolation=Bilinear average create");
run("8-bit");
run("Scale Bar...", "width=&scaleWidth height=6 font=18 color=Black background=None location=[Lower Left] bold overlay");
saveAs("Png", filePath + ".png");
close();
/*
* Create an original-size, 16-bit TIFF file with a scale bar.
*/
run("16-bit");
run("Scale Bar...", "width=&scaleWidth height=12 font=42 color=Black background=None location=[Lower Left] bold");
saveAs("Tiff", filePath + ".tif");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment