// // alpha.ijm // // Simple macro to blend a PNG's alpha channel as you would expect. // Author: Curtis Rueden // November 8, 2011 // Permission is granted to use this for anything you want. // background color over which to composite bgRed = 255; bgGreen = 255; bgBlue = 255; path = File.openDialog("Choose a file"); setBatchMode(true); // open RGB image run("Bio-Formats", "open=" + path + " autoscale color_mode=Composite specify_range view=Hyperstack stack_order=XYCZT c_begin=1 c_end=3 c_step=1"); rgbID = getImageID(); // open alpha channel run("Bio-Formats", "open=" + path + " autoscale color_mode=Grayscale specify_range view=Hyperstack stack_order=XYCZT c_begin=4 c_end=4 c_step=1"); alphaID = getImageID(); // apply alpha to all pixels showStatus("Applying alpha channel..."); for (y = 0; y < getHeight(); y++) { showProgress(y / getHeight()); for (x = 0; x < getWidth(); x++) { selectImage(alphaID); alpha = getPixel(x, y) / 255; selectImage(rgbID); // adjust red component setSlice(1); rValue = getPixel(x, y) * alpha + bgRed * (1 - alpha); setPixel(x, y, rValue); // adjust green component setSlice(2); gValue = getPixel(x, y) * alpha + bgGreen * (1 - alpha); setPixel(x, y, gValue); // adjust blue component setSlice(3); bValue = getPixel(x, y) * alpha + bgBlue * (1 - alpha); setPixel(x, y, bValue); } } selectImage(alphaID); close(); setBatchMode(false);