1

A very similar question that has been answered: How to make a color transparent in a BufferedImage and save as PNG

Unfortunately I couldn't formulate an answer for myself out of that source.

Q: I draw a BufferedImage to my Canvas and would simply like to create a method that turns every pixel with the a certain color (in this case: [214, 127, 255] / 0xD67FFF) into a transparent one. The BufferedImage is of type ARGB.

I do not want to save the BufferedImage as a file, simply display it on my canvas.

Thanks in advance.

Community
  • 1
  • 1
Acidic
  • 6,154
  • 12
  • 46
  • 80

2 Answers2

4

Iterate over all the pixels and perform the check and make transparent.

for (int y = 0; y < image.getHeight(); ++y) {
    for (int x = 0; x < image.getWidth(); ++x) {
         int argb = image.getRGB(x, y);
         if ((argb & 0x00FFFFFF) == 0x00D67FFF)
         {
              image.setRGB(x, y, 0);
         }
    }
}

Make sure the BufferedImage uses an alpha channel, otherwise it will become black.
Note that this will affect your original image.

Edit: Note that I changed the check. Therefor it wouldn't have worked because of I assume your pixels were solid (alpha = 255).


(0xFFD67FFF & 0x00FFFFFF) will result in 0x00D67FFF
And, (0x00D67FFF == 0x00D67FFF)
Martijn Courteaux
  • 67,591
  • 47
  • 198
  • 287
  • This doesn't work. When I check the color of the pixel using `System.out.println(Integer.toHexString(argb))` it returns ffd67fff and not 00d67fff. (alpha value is 255 and not 0, i presume.) I think I don't quite understand your `if` condition. – Acidic Sep 13 '11 at 18:02
  • Yes, indeed. That is why I'm using the mask (using `&`). This way I'm only checking RGB (Not alpha). Alpha doesn't matter in this if. Are you sure it isn't working? – Martijn Courteaux Sep 13 '11 at 18:08
  • Well, I've tried to make it work anyway I could, but to no avail. I did get this to work by modifying the `if` condition the following way: `if (argb == 0xffD67FFF) image.setRGB(x, y, 0x00000000);`. This seems to get the desired effect. Thanks. – Acidic Sep 13 '11 at 18:08
  • Also, `(0xFFD67FFF & 0x00FFFFFF)` results in `d67fff`, while the color is `ffd67fff`. it seems that it should be `(0xFFD67FFF & 0xFFFFFFFF)` though it is the same as `(argb = 0xFFD67FFF)`. – Acidic Sep 13 '11 at 18:17
  • But: the mask will discart the alpha channel, as you know. And what I'm doing is _comparing the `argb` **without alpha** to `0xD67FFF`_ – Martijn Courteaux Sep 13 '11 at 18:20
  • That seems completely logical, but when I check the value of `argb` the alpha isn't discarded, so that seems the be the reason it doesn't work in that form. – Acidic Sep 13 '11 at 18:22
  • Hunk? Are you sure you copied the if statement correctly? Please re-check the statement and try to understand what is happening. Because, I'm pretty sure that I'm correct. :D – Martijn Courteaux Sep 13 '11 at 18:24
  • Upon further check your mask does seem to produce the desired effect. Thanks. :) I wonder what I did wrong... – Acidic Sep 13 '11 at 18:36
0

For a complete solution, like loading, processing and writing, you can use this code:

import java.awt.Color;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;

public class TransparentConverter {

    private static final Color backColor = Color.GREEN;
    private static final int THRESHOLD = 70;
    private static final int TRANSPARENT = 0;  // 0x00000000;

    static File base  = new File("C:\\images");
    static File base2 = new File("C:\\images2");

    public static void main(String[] args) throws IOException {
        System.out.println("TransparentConverter.main()");

        for (File file : base.listFiles()) {
            System.out.println(file);
            BufferedImage initImage = ImageIO.read(file);
            int width = initImage.getWidth(null),
                height = initImage.getHeight(null);

            BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
            Graphics g = image.getGraphics();
            g.drawImage(initImage, 0, 0, null);

            //System.out.println("before: " + image.getRGB(0, 0));
            for (int y = 0; y < height; y++) {
                for (int x = 0; x < width; x++) {
                    int pixel = image.getRGB(x, y);
                    Color color = new Color(pixel);

                    int dr = Math.abs(color.getRed()   - backColor.getRed()),
                        dg = Math.abs(color.getGreen() - backColor.getGreen()),
                        db = Math.abs(color.getBlue()  - backColor.getBlue());

                    if (dr < THRESHOLD && dg < THRESHOLD && db < THRESHOLD) {
                        image.setRGB(x, y, TRANSPARENT);
                    }
                }
            }
            //System.out.println("   after: " + image.getRGB(0, 0));

            file = new File(base2, file.getName());
            //System.out.println("   " + file);
            ImageIO.write(image, "png", file);
        }
    }
}
KitKat
  • 1,495
  • 14
  • 15