image() doesn't need to be called?

edited September 2015 in Questions about Code

If I load an image in setup(), then in draw() it doesn't seem to matter if I invoke the image() method or not. Is there some superclass in which it is called, so my image() call is redundant? Is that, perchance, the same class where pixels is defined? I'd just like a little clarification before I talk about this in class tomorrow, so any info would be appreciated. Needless to say, I've just started looking at processing ...

Answers

  • edited September 2015

    I don't understand.

    when you don't use image() at all, the image is not shown.

    show your code, dude.

  • edited September 2015

    In Processing 3.0b6 the image shows up just fine, and I am not invoking image(). And just like you, Chrisir, I don't understand, hence my question :-)

       `PImage img;
    
        void setup() {    // this method, setup(), is called once 
          size(980, 657); // sets up the size of the display window, 980x657 pixels
          img = loadImage("Nature-Landscape-Winter-Kiev-980x657.jpg"); // loads the image into img
        }
    
        void draw() {    // this is called over and over again (to facilitate animation) ... 
          loadPixels(); // loads pixels from the display window into an array (a numbered list) called, appropriately, pixels
    
          color [] pixelsSorted;
    
          pixelsSorted = sort(pixels);
    
          color minPixel = pixelsSorted[0];
          color maxPixel = pixelsSorted[pixelsSorted.length-1];
    
          for(int i=0; i < img.width*img.height; i++){  // for each of the pixels in the image (0, 1, 2, 3, ... 643859) do the following
             // The functions red(), green(), and blue() pull out the 3 color components from a pixel.
    
             float r = red(img.pixels[i]);
             float g = green(img.pixels[i]);
             float b = blue(img.pixels[i]);
    
             // changing r,g,b, here will change the appearance of the image. For example ...
    
             r = r * 1.1;
             g = g * 1.2;
             b = b * 1.3;
    
             // Set the display pixel to the image pixel
             pixels[i] =  color(r,g,b); // now we've changed the ith pixel's color       
          }
          updatePixels(); // updates the display window with the changed pixels
        }`
    
  • with the for-loop you set the pixels of the screen to the pixels of the image

    hence the image appears.

    image() does the same

  • I appreciate your answer, but I'm still not getting this. According to the docs, loadPixels() loads the pixels array from the display window, and updatePixels() updates the array. That I follow. But how did pixels get its values in the first place?

  • pixels is just an internal array representing your screen

    you make changes on pixels then you make changes on the screen

    with updatePixels() you bring the pixels array onto the screen.

  • Besides pixels[] & image() approaches, set() & background() can also be used to render PImage objects:

    1. https://processing.org/reference/set_.html
    2. https://processing.org/reference/background_.html
  • I guess at the beginning the screen is black and thus pixels with loadPixels(); gets all 0 written in it

Sign In or Register to comment.