Extracting pixels values from an alpha/greyscale PImage?

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.

1 Like

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 … :slight_smile:

2 Likes

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?

Hello @amundsen,

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

Explore these tutorials:
https://processing.org/tutorials/color
https://processing.org/tutorials/pixels

References:
https://processing.org/reference/#color

This is useful to understand color datatype:

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

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!
    }
  }

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

:)

1 Like

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

1 Like

Thanks a lot, @mnse !