# Extracting pixels values from an alpha/greyscale PImage?

**URL:** https://discourse.processing.org/t/extracting-pixels-values-from-an-alpha-greyscale-pimage/38038
**Category:** Beginners
**Created:** [July 20, 2022, 12:11pm UTC](https://discourse.processing.org/t/extracting-pixels-values-from-an-alpha-greyscale-pimage/38038 "2022-07-20T12:11:22Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![amundsen](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/amundsen/32/2497_2.png) [@amundsen](https://discourse.processing.org/u/amundsen)
#### Post date: [July 20, 2022, 12:11pm UTC](https://discourse.processing.org/t/extracting-pixels-values-from-an-alpha-greyscale-pimage/38038/1 "2022-07-20T12:11:22Z")

</div>

Hello.

When using a PImage in alpha/greyscale format, how can I extract individual values from pixels? Should I use alpha(myImage.pixels[pos])? Or red(myImage.pixels[pos])?

Thank you in advance.

---

<div class="post-metadata">

### Author: ![mnse](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/mnse/32/16520_2.png) [@mnse](https://discourse.processing.org/u/mnse)
#### Post date: [July 20, 2022, 4:02pm UTC](https://discourse.processing.org/t/extracting-pixels-values-from-an-alpha-greyscale-pimage/38038/2 "2022-07-20T16:02:38Z")

</div>

Hi @amundsen,

From a low level system perspective I would say, depends on the implementation, but I would assume that processing internally keep PImage as independent from the several Image Implementations.  
Means the grayscale is stored as R=G=B, but alpha can be different. So when you are interested in the alpha values you should use alpha, but if you are interested in the black to white shading, you can go for either red or green or blue …

Cheers  
— mnse

PS: interesting question … I’ll check the proceesing source if I find tsome free time … 🙂

---

<div class="post-metadata">

### Author: ![amundsen](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/amundsen/32/2497_2.png) [@amundsen](https://discourse.processing.org/u/amundsen)
#### Post date: [July 20, 2022, 4:25pm UTC](https://discourse.processing.org/t/extracting-pixels-values-from-an-alpha-greyscale-pimage/38038/3 "2022-07-20T16:25:05Z")

</div>

> [@mnse](#):
>
> PS: interesting question … I’ll check the proceesing source if I find tsome free time … 🙂

Please tell us when you have found the information!

The question is also in the other direction: which coding should one use to modify this PImage?

---

<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: [July 20, 2022, 9:46pm UTC](https://discourse.processing.org/t/extracting-pixels-values-from-an-alpha-greyscale-pimage/38038/4 "2022-07-20T21:46:20Z")

</div>

Hello @amundsen,

Lots of resources (tutorials, references and examples) here:  
[https://processing.org/](https://processing.org/)

Explore these tutorials:  
[https://processing.org/tutorials/color](https://processing.org/tutorials/color)  
[https://processing.org/tutorials/pixels](https://processing.org/tutorials/pixels)

References:  
[https://processing.org/reference/#color](https://processing.org/reference/#color)

This is useful to understand color datatype:

> **[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.…

A fun example I wrote to display the pixel color in hexadecimal notation at the mouse location:

```auto
PImage photo;

void setup() 
  {
  size(200, 255);
  // Image from https://en.wikipedia.org/wiki/Grayscale
  photo = loadImage("https://upload.wikimedia.org/wikipedia/commons/f/fa/Grayscale_8bits_palette_sample_image.png");
  println(photo.width, photo.height);
  image(photo, 0, 0); // Display image
  for (int i = 0; i< 256; i++)
    {
    stroke(i);  
    line(photo.width, i, width, i);
    }
  }

void draw() 
  {
  if (mousePressed)
    {
    println(hex(get(mouseX, mouseY))); // See references!
    }
  }

```

 ![image](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/3X/e/f/ef71e66a8673d0116ea88247979a4271904364b7.png)

I encourage you to start exploring the resources and experiment with writing some code to better understand these topics.

`:)`

---

<div class="post-metadata">

### Author: ![mnse](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/mnse/32/16520_2.png) [@mnse](https://discourse.processing.org/u/mnse)
#### Post date: [July 21, 2022, 9:12am UTC](https://discourse.processing.org/t/extracting-pixels-values-from-an-alpha-greyscale-pimage/38038/5 "2022-07-21T09:12:49Z")

</div>

Hi @amundsen,

check out the (as always) great resources to learn posted by @glv.

As promised, just had a quick look on the processing java source.  
Basically there are 3 Modes (ARGB,RGB (also Grayscale),ALPHA/Greyscale) and as supposed in my first post …

- ARGB: individual values for alpha,red,green,blue [use alpha(),red(),green(),blue() functions]
- RGB: alpha always opaque(FF/255) and individual values for red,green,blue and on grayscale (red=green=blue) [use red(),green(),blue() functions and on grayscale each can be used]
- ALPHA/Grayscale: variable alpha value (basically taken from the red component as assuming red=green=blue) and put full white (FF/255) for red/green/blue on get() function. [so use alpha() function if you using the get() function, but if you are manipulation pixels directly array imo better work on red channel]

Cheers  
— mnse

PS: [Source](https://github.com/processing/processing/blob/master/core/src/processing/core/PImage.java)

---

<div class="post-metadata">

### Author: ![amundsen](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/amundsen/32/2497_2.png) [@amundsen](https://discourse.processing.org/u/amundsen)
#### Post date: [July 21, 2022, 9:13am UTC](https://discourse.processing.org/t/extracting-pixels-values-from-an-alpha-greyscale-pimage/38038/6 "2022-07-21T09:13:39Z")

</div>

Thanks a lot, @mnse !
