Change pixel's color if in condition

edited September 2014 in Questions about Code

Hi there, I'm loading an image into a processing sketch and I would like to change all the black pixels to white. Here's my code, but changing are not having effect, i'm probably doing a wrong use of the set() method.

PImage img; 

void setup() {
  size(800,800);

  img = loadImage("texture.jpg");
}


void draw() {
  loadPixels();
  img.loadPixels();
  img.resize(800,800);
  //loop through every pixel
  for (int x = 1; x < img.width; x++) {
  for (int y = 0; y < img.height; y++ ) {

    // Calculate the 1D pixel location
    int loc = x + y*img.width;

    // Get the R,G,B values from image
    float r = red   (img.pixels[loc]);
    float g = green (img.pixels[loc]);
    float b = blue  (img.pixels[loc]);

    color white = color(255);

    if (r < 5) {
      set(x,y, white);
    } 

    color c = color(r,g,b);
    pixels[loc] = c;
  }
}
updatePixels();
}
Tagged:

Answers

  • Answer ✓

    line 29

    img.set(x,y, white);

  • line 13 should be in setup()

  • line 15

      for (int x = 1;
    

    should be

      for (int x = 0;
    
  • Answer ✓

    line 32+33 delete

    you need to display img

    using image (img,0,0);

  • line 36 img.updatePixels();

  • you have to decide: either you manipulate the image or the screen.

    Better manipulate the image. Then you need to draw it each time. And also you need to have img. before operations like set, get, pixels, updatePixels....

  • maybe look at brightness... for line 28

  • Thanks Chrisir, I've folollowed your suggestions but I'm getting this error: It looks like you're mixing "active" and "static" modes.

  • By the way, I want to replace pixel color and then display the results, I don't want to change brightness.

  • Sorry it actually works putting "image(img,0,0);" right after updatePixels(). Thanks for your help

  • you wrote

    I don't want to change brightness

    yes, I was just referring to the if in line 28

    so it's only detection of the property of the pixel (black?) and not changing it

  • Glad you got it working!

  • hi, may i know what library that you have added for loading the pixel?

  • No need to add any library, just use PImage Class

Sign In or Register to comment.