Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I did that recently, to answer a question of my project manager.<br> The function transforming gray to transparency is:</p> <pre><code> private Image TransformGrayToTransparency(BufferedImage image) { ImageFilter filter = new RGBImageFilter() { public final int filterRGB(int x, int y, int rgb) { return (rgb &lt;&lt; 8) &amp; 0xFF000000; } }; ImageProducer ip = new FilteredImageSource(image.getSource(), filter); return Toolkit.getDefaultToolkit().createImage(ip); } </code></pre> <p>Actually, it acts on a gray-level image, so I just copy a RGB component (the R one) to alpha, discarding the others which are identical in my case.<br> You can adapt it to filter a specific color, eg. with a test of equality or range, etc.<br> Of course, the BufferedImage must be of BufferedImage.TYPE_INT_ARGB type.</p> <p>I don't address the question of saving, as it is pretty trivial, but I can add this code page too.</p> <p>[EDIT] To convert Image to BufferedImage:</p> <pre><code>BufferedImage dest = new BufferedImage( imageWidth, imageHeight, BufferedImage.TYPE_INT_ARGB); Graphics2D g2 = dest.createGraphics(); g2.drawImage(image, 0, 0, null); g2.dispose(); </code></pre> <p>[EDIT 2] I come after Christoffer posted his complete solution, but here is mine, I show how to make a range of colors transparent. Can be improved, eg. using HSB components instead.</p> <pre><code>import java.awt.*; import java.awt.image.BufferedImage; import java.awt.image.FilteredImageSource; import java.awt.image.ImageFilter; import java.awt.image.ImageProducer; import java.awt.image.RGBImageFilter; import java.io.*; import javax.imageio.ImageIO; public class AddTransparency { AddTransparency() throws IOException { String imagePath = "E:/Documents/images/"; File inFile = new File(imagePath, "map.png"); BufferedImage image = ImageIO.read(inFile); Image transpImg1 = TransformGrayToTransparency(image); BufferedImage resultImage1 = ImageToBufferedImage(transpImg1, image.getWidth(), image.getHeight()); File outFile1 = new File(imagePath, "map_with_transparency1.png"); ImageIO.write(resultImage1, "PNG", outFile1); Image transpImg2 = TransformColorToTransparency(image, new Color(0, 50, 77), new Color(200, 200, 255)); BufferedImage resultImage2 = ImageToBufferedImage(transpImg2, image.getWidth(), image.getHeight()); File outFile2 = new File(imagePath, "map_with_transparency2.png"); ImageIO.write(resultImage2, "PNG", outFile2); } private Image TransformGrayToTransparency(BufferedImage image) { ImageFilter filter = new RGBImageFilter() { public final int filterRGB(int x, int y, int rgb) { return (rgb &lt;&lt; 8) &amp; 0xFF000000; } }; ImageProducer ip = new FilteredImageSource(image.getSource(), filter); return Toolkit.getDefaultToolkit().createImage(ip); } private Image TransformColorToTransparency(BufferedImage image, Color c1, Color c2) { // Primitive test, just an example final int r1 = c1.getRed(); final int g1 = c1.getGreen(); final int b1 = c1.getBlue(); final int r2 = c2.getRed(); final int g2 = c2.getGreen(); final int b2 = c2.getBlue(); ImageFilter filter = new RGBImageFilter() { public final int filterRGB(int x, int y, int rgb) { int r = (rgb &amp; 0xFF0000) &gt;&gt; 16; int g = (rgb &amp; 0xFF00) &gt;&gt; 8; int b = rgb &amp; 0xFF; if (r &gt;= r1 &amp;&amp; r &lt;= r2 &amp;&amp; g &gt;= g1 &amp;&amp; g &lt;= g2 &amp;&amp; b &gt;= b1 &amp;&amp; b &lt;= b2) { // Set fully transparent but keep color return rgb &amp; 0xFFFFFF; } return rgb; } }; ImageProducer ip = new FilteredImageSource(image.getSource(), filter); return Toolkit.getDefaultToolkit().createImage(ip); } private BufferedImage ImageToBufferedImage(Image image, int width, int height) { BufferedImage dest = new BufferedImage( width, height, BufferedImage.TYPE_INT_ARGB); Graphics2D g2 = dest.createGraphics(); g2.drawImage(image, 0, 0, null); g2.dispose(); return dest; } public static void main(String[] args) throws IOException { AddTransparency at = new AddTransparency(); } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload