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

// Example 15-7: Displaying the pixels of an image

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 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 three 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
      // If we were to change the RGB values, we would do it here, before setting the pixel in the display window.

      // Set the display pixel to the image pixel
      pixels[loc] = color(r,g,b);
    }
  }
  
  updatePixels();
}
  • random

    If the image source has not the same dimension of the display area… where i put “int imageLoc = x + y*img.width;”, and where “int displayLoc = x + y*width;”

  • Anonymous

    Exactly, you would say:

    int displayLoc = x + y*width;
    int imageLoc = x + y*img.width;
    pixels[displayLoc] = img.pixels[imageLoc];

  • random

    Thank you very very much Daniel!!! Now is clear… ;)
    P.S: thank for your quick response.

  • HANDIKA ERANDO

    for (int y = 0; y < height; y++ ) {
    for (int x = 0; x < width; x++ ) {
    int loc = x + y*width; int displayLoc = x + y*width; int imageLoc = x + y*img.width; pixels[displayLoc] = img.pixels[imageLoc];
    float r = red(img.pixels [loc]);
    float g = green(img.pixels[loc]);
    float b = blue(img.pixels[loc]);

    pixels[loc] = color(r,g,b);
    }
    }Where i must insert that code…i am already trying implements that code like above…but in row float r … blabla error in arrayindexoutofbound exception 40000

  • Anonymous

    Make sure your image is the same dimensions as your Processing window, i.e. 

    size(640,480);

    your image should be 640×480

  • Emcmalik

    when i read all the values of RGB and store them in .txt file… how to read back same file to create same image…is it possible

  • Anonymous

    yes, you can using loadStrings() and saveStrings().  But you can also just use save() to save an image out as a JPG, PNG, or TIFF for loading later!

  • Emcmalik

    how to read char by char from txt file.. what and all operation can we do on char..

  • earlgray

    do you happen to know how to load pixels from two different images? thank you!

  • http://www.facebook.com/rania.raval Rania Raval

    plz plzzzzzzzz tel me how to load image in pixels bihind show matrix in coding and and which location which pixels where i write code

  • http://www.facebook.com/rania.raval Rania Raval

    m new commer learning c#.net and load image in pixls try to send me fast reply
    m waiting

  • http://www.facebook.com/rania.raval Rania Raval

    m waitng

  • Ariel

    Why is there a need for img.loadPixels? Since the code can run without it being there.