How to change the color of each pixel in an image?
in
Programming Questions
•
7 months ago
Hi everyone. I'm new to Processing and I'm desperate for help.
I need help with changing image colors by looping through each pixel. I can't use tint(). Basically, I need to color all the green pixels red, and all the red pixels green in response to a mouse click.
Your help will be much appreciated.
- PImage img;
- void setup() {
- img = loadImage("img.jpg");
- size(img.width,img.height);
- }
- void draw() {
- loadPixels();
- img.loadPixels();
- for (int y = 0; y < height; y++) {
- for (int x = 0; x < width; x++) {
- int loc = x + y*width;
- // The functions red(), green(), and blue() pull out the 3 color components from a pixel.
- float r = red(img.pixels[loc]);
- float g = green(img.pixels[loc]);
- float b = blue(img.pixels[loc]);
- // Image Processing would go here
- // Set the display pixel to the image pixel
- pixels[loc] = color(r,g,b);
- }
- }
- updatePixels();
- }
1