Newbie - Confused about images and their pixels
in
Programming Questions
•
2 years ago
I'm trying to load an colored image and change it to grayscale.
I want to change all the rgb color values into something that is grayscale.
The formula for doing this is supposedly take 30% of the red component, 59% from the green component, and 11% from the blue component.
I was thinking a could change the pixels array so I tried something that looks like this:
-
PImage work;
-
//...setup and instantiated work
-
void grayscale(){
work.loadPixels();
PImage grey = work;
grey.loadPixels();
for(int i = 0; i< pixels.length; i++){
color cp = get(pixels[i]);
color cpp = (int)(.3*red(cp)+ .59 *green(cp)+ .11*blue(cp));
grey.pixels[i] = color(cpp);
}
grey.updatePixels();
}
It doesn't work, in fact it just displays the exact image over it. I don't know what is wrong.
EDIT:
I suppose the easiest thing for me to do is to load all the image color values into an array of integers that correlate to their proper grayscale color value and then construct a new image from that.
1