Example
// Learning Processing
// Daniel Shiffman
// http://www.learningprocessing.com

// Example 15-8: Adjusting image brightness

PImage img;

void setup() {
  size(200,200);
  img = loadImage("sunflower.jpg");
}

void draw() {
  loadPixels();

  // We must also call loadPixels() on the PImage since we are going to read its pixels.  img.loadPixels(); 
  for (int x = 0; 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]);

      // We calculate a multiplier ranging from 0.0 to 8.0 based on mouseX position. 
      // That multiplier changes the RGB value of each pixel.      
      float adjustBrightness = ((float) mouseX / width) * 8.0; 
      r *= adjustBrightness;
      g *= adjustBrightness;
      b *= adjustBrightness;

      // The RGB values are constrained between 0 and 255 before being set as a new color.      
      r = constrain(r,0,255); 
      g = constrain(g,0,255);
      b = constrain(b,0,255);

      // Make a new color and set pixel in the window
      color c = color(r,g,b);
      pixels[loc] = c;
    }
  }
  
  updatePixels();  
}
  • Matthew M

    Thanks for all of the examples. I keep running into a ArrayIndexOutOfBoundsException: 40320 error when I try and run this code example. I originally just tried to adapt the previous code to enhance my knowledge, but when that failed I ran this code (cut and paste) but added my own test image. I was able to load and use a test image with prior code examples.

    Any suggestions? I am using Processing 1.2.1 on a Mac.

    The line it highlights is ” pixels[loc] = c;”

  • http://www.learningprocessing.com Daniel Shiffman

    It’s likely your image file isn’t the same resolution as your Processing sketch. i.e. if you have

    size(600,400);

    your image needs to also be 600×400 (for this example to run). Otherwise, you’d have to alter the math and use a different “loc” for the Processing window and the image pixels.

  • http://www.mary-murray.com Slopeavery

    I tried to view the value for “r” using println(r) it freezes the program, I’m not sure why.

      When I make “r” a global variable I see numbers close to what I expect if a place the code after updatePixels();

         updatePixels();
      //println(“r is ” + r);

    If I put it in the body of the code it stops working, don’t get that either.

    Thanks for the book! I’m enjoying it.

  • Anonymous

    There is an ‘r’ for every single pixel (640 *480!) so you are printing  307,200 lines every time through draw, that’s too much for the message console to handle.  Try just printing out the center value or something.

    if (x == 100 && y == 100) {
      println(r);
    }

  • Anonymous

    It’s likely your image file isn’t the same resolution as your Processing sketch. i.e. if you have

    size(600,400);

    your image needs to also be 600×400 (for this example to run). Otherwise, you’d have to alter the math and use a different “loc” for the Processing window and the image pixels. 

  • Michael

    I understand why we use adjustBrightness, but I’m unclear as to how we calculated it; specifically why we multiply mouseX by float. Also, why not use mouseX as-is? Why create a fraction then on top of that figure in a multiplier (0–8)? Thanks Dan!

  • Michael

    Pls. disregard the post below, after println’ing adjustBrightness, I now understand we’re adjusting the entire image from dark to light, rather than just brightening what exists.

  • Lpcambra

    Hi, Mr.Shiffman

    Just tried to do the same code as above, but i’m gettin the same error over and over…

             ”  Could not find a method to load lomotomocopy
                Exception in thread “Animation Thread” java.lang.NullPointerException”

    I’m running it on a windows7 64, on the Processing version 1.5.1

    i’m very “noob” at this…

  • Anonymous

    sorry, i’d need to know more of the error.  did you make sure you have the image file in your data folder?  try downloading the example and run that directly rather than re-typing it.