Processing: Logic Operators and Image Processing

During the lesson we also learned about logic operators. Logical operators allow the programmer to connect two arguments together. They are fundamental to all programming language and allow more complex programs to be constructed.

The three main logical operators are:

! NOT

!a means NOT a

&& AND

a && b means a and b

|| OR

a || b means a or b

We also learned about image processing using PImage to import photographs. Here I used PImage to import two photographs, one of a Papillon and one of a Shiba Inu. I then used a for loop to access all the pixels on the screen and return their colour value. The pixels are then offset by being redrawn with a random value added to their location. This gives the image a “jiggly” effect.

I then added some interactive functionality by changing the picture if certain parameters are set. If the left mouse button is held down or a key is pressed, the image changes to that of the shiba inu. However if neither of those conditions are met, the picture is that of the papillon dog. I also used the text command to write to the canvas.

//int image
PImage img1;
PImage img2;
int i;
int j;
int s;


void setup(){
  //load image
 img1 = loadImage("papillon.jpg");
 img2 = loadImage("shiba.jpg");
 //make canvas image size
 size(img2.width, img2.height);
}



void draw(){
  s = 1; 
  

for (i = 0; i < width; i = i + s){
         for (j = 0; j < height; j = j+ s){
          //find pixel location
           int loc = i + (j* width);
           //return pixel colour at pixel location
           color c = img1.pixels[loc];
          noStroke();
         // stroke(c, 100);
           fill(c, 20);
          rect(i+random(-10, 10),j+random(-10, 10), s, s);
            
    }
  }
  
  if (mousePressed && ( mouseButton == LEFT || keyPressed) ){
    img1= loadImage("shiba.jpg");
  }
  else{
    textSize(30);
    text("Papillon", 100, 100);
    img1= loadImage("papillon.jpg");
  }
}

Papillon

The image if no keyboard keys or the left mouse button are pressed.

Shiba

The image if they are.

Processing.org, 2014. LogicalOperators \ Examples \ Processing.org. [online] Available at: https://processing.org/examples/logicaloperators.html [Accessed 18 Nov. 2014].

Leave a Comment