1

Im trying to get the amount of pixels that is transparent in a png through Processing by using the get() command or img.get(). I've tried putting a white background and use this code but the output isn't near what it should be for the picture I test with which I know is filled with about 50% black and rest transparent.

I used this code and expected to get value of "v" too land in the 125xxxx region since I know approx 50% of the picture femtiofemtio.png is black:

PImage img;
color currentColor;

void setup() {
  size(1176,2172);
  currentColor = color(0);
  img = loadImage("femtiofemtio.png");
}

void draw() {
  int v = 0;
  background(255);
  image(img, 0, 0);
  for (int x = 1; x < 1176; x = x+1) {
  for (int y = 1; y < 2172; y = y+1) {
  currentColor = img.get(x,Y);
  if (alpha(currentColor)==255){
    v=v+1;
    println(v);
   }
}
}
  //print(v/2554272);
  print("end    ");
  exit();
}

1 Answers1

1

the problem is located in line 16 of your code snippets. you are mistakenly using an upper case Y instead of the lower case y. the line should look like this:

  currentColor = img.get(x, y);

the reason your code still compiles and runs is that the upper case Y is an existing constant defined by the processing environment in PConstants ( with the value 1 ). it is usually used in combination with indices ( e.g vertex[Y] = 1.0 ).