How to Check If a Specific Pixel of an Image Is Transparent

How to check if a specific pixel of an image is transparent?

Building on Jeff's answer, your first step would be to create a canvas representation of your PNG. The following creates an off-screen canvas that is the same width and height as your image and has the image drawn on it.

var img = document.getElementById('my-image');
var canvas = document.createElement('canvas');
canvas.width = img.width;
canvas.height = img.height;
canvas.getContext('2d').drawImage(img, 0, 0, img.width, img.height);

After that, when a user clicks, use event.offsetX and event.offsetY to get the position. This can then be used to acquire the pixel:

var pixelData = canvas.getContext('2d').getImageData(event.offsetX, event.offsetY, 1, 1).data;

Because you are only grabbing one pixel, pixelData is a four entry array containing the pixel's R, G, B, and A values. For alpha, anything less than 255 represents some level of transparency with 0 being fully transparent.

Here is a jsFiddle example: http://jsfiddle.net/thirtydot/9SEMf/869/ I used jQuery for convenience in all of this, but it is by no means required.

Note: getImageData falls under the browser's same-origin policy to prevent data leaks, meaning this technique will fail if you dirty the canvas with an image from another domain or (I believe, but some browsers may have solved this) SVG from any domain. This protects against cases where a site serves up a custom image asset for a logged in user and an attacker wants to read the image to get information. You can solve the problem by either serving the image from the same server or implementing Cross-origin resource sharing.

Can i know if an image is transparent at a specific position?

'Burn' the image into an HTML5 canvas, then get the specific pixel from the canvas and check its Alpha (http://falcon80.com/HTMLCanvas/PixelManipulation/getImageData.html).

I think it would be easier if you only worked with HTML5 canvas instead of moving around DOM segments.

Finding transparent pixels on an image and making same pixels transparent on another image

You can use the tattered image as mask for other images as well. You just need the alpha information from the mask.

Implementation of creating tattered borders using BufferedImage:

public class Test {

public static void main(String[] args) throws IOException {

BufferedImage mask = loadImage("e:\\mask.png");
BufferedImage img = loadImage("e:\\1.png");

int width = mask.getWidth();
int height = mask.getHeight();

BufferedImage processed = new BufferedImage(width, height, BufferedImage.TYPE_4BYTE_ABGR);

for (int x = 0; x < width; x++) {
for (int y = 0; y < height; y++) {
int rgb = mask.getRGB(x,y);
int maskAlpha = alpha(rgb);
int imgColor = img.getRGB(x, y);
if (maskAlpha < 255) {
processed.setRGB(x,y,maskAlpha(imgColor, maskAlpha));
} else {
processed.setRGB(x,y,imgColor);
}
}
}

writeImage(processed, "e:\\2.png");
}

static BufferedImage loadImage(String imagePath) {
File file = new File(imagePath);
BufferedImage image = null;
try {
image = ImageIO.read(file);
} catch (IOException e) {
e.printStackTrace();
}
return image;
}

static void writeImage(BufferedImage img,String filePath){
String format = filePath.substring(filePath.indexOf('.')+1);
//Get Picture Format
System.out.println(format);
try {
ImageIO.write(img,format,new File(filePath));
} catch (IOException e) {
e.printStackTrace();
}
}

static int maskAlpha(int rgb, int alpha) {
//strip alpha from original color
int color = (0x00ffffff&rgb);
return color + ((alpha)<<24);
}

static int alpha(int rgb) {
return (0xff&(rgb>>24));
}

static int red(int rgb) {
return (0xff&rgb);
}
static int green(int rgb) {
return (0xff&(rgb>>8));
}
static int blue(int rgb) {
return (0xff&(rgb>>16));
}
}

Here BufferedImage.TYPE_4BYTE_ABGR means

Represents an image with 8-bit RGBA color components with the colors
Blue, Green, and Red stored in 3 bytes and 1 byte of alpha. The image
has a ComponentColorModel with alpha. The color data in this image is
considered not to be premultiplied with alpha. The byte data is
interleaved in a single byte array in the order A, B, G, R from lower
to higher byte addresses within each pixel.

That means color integer is 32 bits, which is stored in java in the order of abgr, i.e., alpha is the first 8 bits, and r is the last 8 bits, so you can get the argb value as follows:

int r = (0xff&rgb);
int g = (0xff&(rgb>>8));
int b = (0xff&(rgb>>16));
int a = (0xff&(rgb>>24));

Fastest way to check if an image is all white or all transparent

Another idea could be:

1 - scale the picture down to a very small size (say 100 by 100 px).

2 - analyze each pixel color in a nested for loop (for y including for x - or vice versa).

3 - if all these 10000 px have an alpha of 0 (just use a counter you increment every time the examined pixel's alpha is 0), you can affirm with good approximation that the image is completely transparent.

How to check if an image has transparency using GD?

It doesn't look like you can detect transparency at a glance.

The comments on the imagecolorat manual page suggest that the resulting integer when working with a true-color image can actually be shifted four times total, with the fourth being the alpha channel (the other three being red, green and blue). Therefore, given any pixel location at $x and $y, you can detect alpha using:

$rgba = imagecolorat($im,$x,$y);
$alpha = ($rgba & 0x7F000000) >> 24;
$red = ($rgba & 0xFF0000) >> 16;
$green = ($rgba & 0x00FF00) >> 8;
$blue = ($rgba & 0x0000FF);

An $alpha of 127 is apparently completely transparent, while zero is completely opaque.

Unfortunately you might need to process every single pixel in the image just to find one that is transparent, and then this only works with true-color images. Otherwise imagecolorat returns a color index, which you must then look up using imagecolorsforindex, which actually returns an array with an alpha value.

Java BufferedImage how to know if a pixel is transparent

BufferedImage img = ....

public boolean isTransparent( int x, int y ) {
int pixel = img.getRGB(x,y);
if( (pixel>>24) == 0x00 ) {
return true;
}
return false;
}

Of course img has to be in the correct format TYPE_4BYTE_ABGR or some format that supports alpha channels else if will always be opaque (ie 0xff).



Related Topics



Leave a reply



Submit