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

// Example 15-14: "Pointillism"

PImage img;
int pointillize = 16;

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

void draw() {
  
  // Pick a random point
  int x = int(random(img.width));
  int y = int(random(img.height));
  int loc = x + y*img.width;
  
  // Look up the RGB color in the source image
  loadPixels();
  float r = red(img.pixels[loc]);
  float g = green(img.pixels[loc]);
  float b = blue(img.pixels[loc]);
  
  // Back to shapes! Instead of setting a pixel, we use the color from a pixel to draw a circle.
  noStroke();
  fill(r,g,b,100);
  ellipse(x,y,pointillize,pointillize); 
}
  • Yahya

    It seems that several comments in these examples are misplaced. For example, in this code, we have:
    noStroke();

    // Draw an ellipse at that location with that color
    fill(r,g,b,100);

    Surely the purpose of the call to noStroke() is to set up the fill()? In other languages, we might have something like:
    // Draw an ellipse at that location with that color and specified stroke parameters
    fill(r,g,b,100,strokeRed, strokeGreen, strokeBlue, strokeAlpha, strokeWidth);
    so the “noStroke()” is really equivalent simply to “strokeWidth = 0″. Placing the “noStroke()” after the comment that introduces the ellipse drawing block has another advantage: the necessary code steps are less likely to get separated by mistake, thus:

    // Draw an ellipse at that location with that color
    noStroke();
    fill(r,g,b,100);

    The blank line before the comment also helps to delineate the entire ellipse drawing block more clearly.

  • admin

    Thanks for the correction.

  • commandz

    Hi Daniel,

    Why is there no updatePixels() here?

  • shiffman

    We only need to read the pixels we don’t need to write to then.

  • commandz

    Thank you!

  • 骏 张

    I try to show the whole picture form the begining instead of show up in random locations,but it seems could not work:(,Give me a hand.

    PImage photo;
    void setup() {
    size(800, 567);
    photo=loadImage(“test2.jpg”);
    background(255);
    smooth();
    }
    void draw() {
    for (int i = 0; i < photo.width; i++) {
    for (int j = 0; j < photo.height; j++ ) {
    int all=i+j*photo.width;
    loadPixels();
    float r=red(photo.pixels[all]);
    float g=green(photo.pixels[all]);
    float b=blue(photo.pixels[all]);
    noStroke();
    fill(r, g, b, 150);
    ellipse(i, j, 20, 20);
    }
    }
    }

  • shiffman

    remove loadPixels(). also try increment i and j by 20.

    i+=20;

  • 骏 张

    TKS~