# Get alpha value from pixels array

**URL:** <https://discourse.processing.org/t/get-alpha-value-from-pixels-array/33479>\
**Category:** Coding Questions\
**Tags:** homework\
**Created:** [November 12, 2021, 10:48pm UTC](https://discourse.processing.org/t/get-alpha-value-from-pixels-array/33479 "2021-11-12T22:48:53Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![bobodoustaud](https://avatars.discourse-cdn.com/v4/letter/b/91b2a8/32.png) [@bobodoustaud](https://discourse.processing.org/u/bobodoustaud)\
**Post date:** [November 12, 2021, 10:48pm UTC](https://discourse.processing.org/t/get-alpha-value-from-pixels-array/33479/1 "2021-11-12T22:48:53Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![hotfooted](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/hotfooted/32/3670_2.png) [@hotfooted](https://discourse.processing.org/u/hotfooted)\
**Post date:** [November 13, 2021, 2:21am UTC](https://discourse.processing.org/t/get-alpha-value-from-pixels-array/33479/2 "2021-11-13T02:21:36Z")

</div>

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.

> **[alpha() / Reference](https://processing.org/reference/alpha_.html)**
>
> Extracts the alpha value from a color.

some scrap sample

```auto
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!

---

<div class="post-metadata">

**Author:** ![glv](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/glv/32/18785_2.png) [@glv](https://discourse.processing.org/u/glv)\
**Post date:** [November 13, 2021, 3:27am UTC](https://discourse.processing.org/t/get-alpha-value-from-pixels-array/33479/3 "2021-11-13T03:27:50Z")

</div>

Hello,

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

> **[Welcome to Processing!](https://processing.org/)**
>
> Processing is a flexible software sketchbook and a language for learning how to code. Since 2001, Processing has promoted software literacy within the visual arts and visual literacy within technology…

> [@bobodoustaud](#):
>
> i realise that it is missing the alpha values.

It is there.

> **[color / Reference](https://processing.org/reference/color_datatype.html)**
>
> Datatype for storing color values. Colors may be assigned with get() and color() or they may be specified directly using hexadecimal notation such as #FFCC00 or 0xFFFFCC00.…

`:)`

---

<div class="post-metadata">

**Author:** ![bobodoustaud](https://avatars.discourse-cdn.com/v4/letter/b/91b2a8/32.png) [@bobodoustaud](https://discourse.processing.org/u/bobodoustaud)\
**Post date:** [November 13, 2021, 11:15pm UTC](https://discourse.processing.org/t/get-alpha-value-from-pixels-array/33479/4 "2021-11-13T23:15:14Z")

</div>

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:

```auto
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.
