Get alpha value from pixels array

So i opened a png with transparent pixels in a new PGraphics object. If i load it and render it in the main canvas, it has transparency, which means that the PGraphics background is transparent.
But if i create a pixel array with loadPixels, i realise that it is missing the alpha values. I already sweeped my google research page and found nothing with the keywords “alpha from pixels array processing”.

My ultimate goal with this is to change pixels in a pixel range and replace them with an unified color (like andy warhol’s pop arts) but leaving the transparency untouched)

Tell me if you need some code, i’ll provide it. Thanks in advance!

i’m probably not understanding you correctly and you should always paste code. the more information the better.

The pixels[] array contains the values for all the pixels in the display window. These values are of the color datatype.

the color type is really just a packed integer and there are helper functions to get the specific channel you want from the color. or you can do it yourself. either way it doesn’t matter. but the alpha helper function is probably what you want.

some scrap sample

PImage img;
PGraphics buffer;
int howManyPixelsToLog;
int pixelsLogged;

void setup() {
  size(512, 512);
  
  img = loadImage("cat.png");
  int maxWidth = min(width, img.width);
  int maxHeight = min(height, img.height);
  float r = min(maxWidth / (float)img.width, maxHeight / (float)img.height);
  img.resize(floor(img.width * r), floor(img.height * r));
  
  buffer = createGraphics(width, height);
  
  //draw some random ish
  buffer.beginDraw();
  buffer.image(img, 0, 0);
  buffer.fill(255, 0, 0);
  buffer.rect(16, 16, width * .64, height * .46);
  buffer.fill(255, 255, 0);
  buffer.ellipse(buffer.width / 2, buffer.height / 2, width * .34, width * .34);
  buffer.stroke(0, 255, 255);
  buffer.line(16, 32, width * .8, height * .75);
  buffer.endDraw();
  
  //set ~25 percent of all pixels to zero alpha
  buffer.loadPixels();
  for(int y = 0; y < buffer.height; y++) {
    for(int x = 0; x < buffer.width; x++) {
      int xy = (x + y * buffer.width);
      if(random(1) < .25) {
        //you can ignore the comments below they are just an interesting little thing i noticed
        //but basically we're just setting some of the pixels to have zero alpha
        //and there is multiple ways of doing so
        
        //apparently it is packed as argb ? yet this doesn't work?
        //buffer.pixels[xy] |= 0 << 24;
        //but this does work?
        buffer.pixels[xy] = 0 | buffer.pixels[xy] >> 16 & 255 | buffer.pixels[xy] >> 8 & 255 | buffer.pixels[xy] & 255;
        /*as does this as expected
        float r = red(buffer.pixels[xy]);
        float g = green(buffer.pixels[xy]);
        float b = blue(buffer.pixels[xy]);
        buffer.pixels[xy] = color(r, g, b, 0);
        */
      }
    }
  }
  buffer.updatePixels();
  
  howManyPixelsToLog = 50;
  pixelsLogged = 0;
}

void draw() {
  background(127, 0, 127);
  
  image(buffer, 0, 0);
  
  //if the logging is completed stop the loop and return
  if(pixelsLogged++ > howManyPixelsToLog) {
    noLoop();
    return;
  }
  
  //log some amount of random pixels information
  //to show that the alpha is available
  buffer.loadPixels();
  int someRandomX = floor(random(0, buffer.width));
  int someRandomY = floor(random(0, buffer.height));
  color c = buffer.get(someRandomX, someRandomY);//lazy
  float red = red(c);
  float green = green(c);
  float blue = blue(c);
  float alpha = alpha(c);
  println("red " + red + ", green " + green + ", blue " + blue + ", alpha " + alpha);
}

now that i re-read your question if you don’t plan on changing the alpha values why does it matter whether they are “missing”? idk. have a good one!

1 Like

Hello,

Lots of resources here and a tutorial on pixels and images:

It is there.

:)

ok thanks !
i got it to work.

I just filtered the pixels by excluding transparent pixels. To get the alpha value of a pixel in a array, i did:

for i in range(x):
    pixels[i]>>24&0xFF

On the other side, i encountered an oddity with the python mode for processing, so i will create a new post.

3 Likes