/** * Open, modify, save and show a PNG image with an alpha * channel. This script's purpose is to demontrate how this * can be done and where ImageJ needs to be bypassed. For * real data processing, I suggest to transfer it into a * Java plugin as single pixel processing is prohibitively * slow from Beanshell. * * Requires ImageJ 1.47o or later. * * @author Stephan Saalfeld */ import javax.imageio.ImageIO; import java.awt.image.BufferedImage; /* open a PNG with alpha into a ColorProcessor */ path = IJ.getFilePath("Open PNG with alpha"); cp = new ColorProcessor(ImageIO.read(new File(path))); w = cp.getWidth(); h = cp.getHeight(); /* get the alpha channel and punch a hole into it */ bpAlpha = cp.getChannel(4, null); r = Math.min(w, h)/4; roi = new OvalRoi(w/2-r, h/2-r, 2*r, 2*r); bpAlpha.setValue(0); bpAlpha.fill(roi); /* restore the modified alpha channel and create a BufferedImage */ cp.setChannel(4, bpAlpha); cp2Image = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB); raster = cp2Image.getRaster(); raster.setDataElements(0, 0, w, h, cp.getPixels()); /* save and show the modified image */ path2 = path + "-modified.png"; ImageIO.write( cp2Image, "png", new File(path2)); IJ.open(path2);