Loading...
Logo
Processing Forum
I tried to posterize a picture but when i clicked the mouse for the effect to happen. the error appears: arrayindexoutofboundsexception

the program is like this:
int i;
float total, r, g, b;
PImage img;

void setup(){
  size(427,640);
  background(255);
 img=loadImage("Hey.jpg");
 image(img, 0, 0);
 
}
void draw(){
   
}
void mouseClicked(){
  img.loadPixels();
  for(i=0; i<img.pixels.length; i++){
    r=red(img.pixels[i]);
    g=green(img.pixels[i]);
    b=blue(img.pixels[i]);}
   total=r+g+b;
  if (total<182)
  img.pixels[i]=color(0, 51, 76);
  else if (total<364)
  img.pixels[i]=color(217, 26, 33);
   else if (total<546)
  img.pixels[i]=color(112,150,158);
   else
  img.pixels[i]=color(252, 227, 166);
 
  img.updatePixels();}

Replies(1)

you had 2 problems that prevent your code from working.

the first is that you dont loop through all the pixels.
check where your for loop is closed.

and after updating the image pixels, you need to draw the image again to see it.


just made the changes.

Copy code
  1. int i;
    float total, r, g, b;
    PImage img;

    void setup(){
      size(427,640);
      background(255);
     img=loadImage("folder.jpg");
     image(img, 0, 0);
     
    }
    void draw(){
      
    }
    void mouseClicked(){
      img.loadPixels();
      for(i=0; i<img.pixels.length; i++){
        r=red(img.pixels[i]);
        g=green(img.pixels[i]);
        b=blue(img.pixels[i]);
       total=r+g+b;
      if (total<182)
      img.pixels[i]=color(0, 51, 76);
      else if (total<364)
      img.pixels[i]=color(217, 26, 33);
       else if (total<546)
      img.pixels[i]=color(112,150,158);
       else
      img.pixels[i]=color(252, 227, 166);
     }
      img.updatePixels();
      image(img,0,0);}