I have a gif image which contains only a colored shape, and a transparent background.
I would like to replace the shape's color by the one I want (the color pallet for this gif is only 2 colors: transparent and white in my case).
I've created a filter which correctly replace white with red (this is a test).
However I'm encountering an issue with my method imageToBufferedImage, it removes the transparency and replace it with black (don't know why).
So what I've done so far is this:
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Toolkit;
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.File;
import javax.imageio.ImageIO;
public class TestPNG {
public static void main(String[] args) throws Exception {
File in = new File("bg.gif");
BufferedImage source = ImageIO.read(in);
int color = source.getRGB(0, 0);
Image image = makeColorTransparent(source, new Color(color), new Color(255, 0, 0));
BufferedImage transparent = imageToBufferedImage(image);
File out = new File("bg2.gif");
ImageIO.write(transparent, "gif", out);
}
private static BufferedImage imageToBufferedImage(Image image) {
BufferedImage bufferedImage = new BufferedImage(image.getWidth(null), image.getHeight(null), BufferedImage.TYPE_INT_ARGB);
Graphics2D g2 = bufferedImage.createGraphics();
//g2.setBackground(Color.blue);
g2.clearRect(0, 0, 200, 40);
g2.drawImage(image, 0, 0, null);
g2.dispose();
return bufferedImage;
}
public static Image makeColorTransparent(BufferedImage im, final Color search, final Color replace) {
ImageFilter filter = new RGBImageFilter() {
public final int filterRGB(int x, int y, int rgb) {
if (rgb == search.getRGB()) {
return replace.getRGB();
} else {
return rgb;
}
}
};
ImageProducer ip = new FilteredImageSource(im.getSource(), filter);
return Toolkit.getDefaultToolkit().createImage(ip);
}
}
There are 3 problems in your code:
1) Replace
Image image = makeColorTransparent(source, new Color(color), new Color(255, 0, 0));
with
Image image = makeColorTransparent(source, color, new Color(255, 0, 0));
and
public static Image makeColorTransparent(BufferedImage im, final Color search, final Color replace) {
...
if (rgb == search.getRGB()) {
...
}
with
public static Image makeColorTransparent(BufferedImage im, final int search, final Color replace) {
...
if (rgb == search) {
...
}
BECAUSE for some reason, the source.getRGB(0, 0)
ignores alpha value and it becomes white ((255, 255, 255, 0) becomes (255, 255, 255, 255))
2) You can't use int color = source.getRGB(0, 0)
, because it uses the color of first pixel (transparent). You should use some other code (like asking for the color in console) to find out what pixel's color to store in int color
3) You are clearing the color of BufferedImage bufferedImage
in imageToBufferedImage(...)
with Color.BLACK
(default). Replace //g2.setBackground(Color.blue);
with g2.setBackground(new Color(0, 0, 0, 0));
or remove the g2.clearRect(...);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With