Reputation: 371
I am trying to write a code where I get a png/jpeg image. If it is a png image, I want to check if it's background is 100% transparent. If yes, I want to add white background using image magick.
Currently I use image magick's "identify -format %A new.png" which returns true or false based on transparency.
However, is there any way to find out 100 percent background transparency using image magick or java code?
Upvotes: 1
Views: 17955
Reputation: 619
You could iterate over each pixel in the image and check whether the most significant byte (which is the alpha channel) is zero (as explained here). Do it somehow like this:
public static boolean isFullyAlpha(File f) throws IOException {
BufferedImage img = ImageIO.read(f);
for(int y = 0; y < img.getHeight(); y++) {
for(int x = 0; x < img.getWidth(); x++) {
if(((img.getRGB(x, y) >> 24) & 0xFF) != 0) {
return false;
}
}
}
return true;
}
Upvotes: 1