All Posts

Kyle plays with 3D pixels and may or may not have created an image of God

I worked with a friend to learn how to take an image that I had grabbed color values from and pixelated, and make each of the pixels into a 3-dimensional box. we used “brightness” to sense how bright each square was and then correlated that to the magnitude of the pixel’s height.

I also correlated the zoom and overall pixel height to mouse X and mouse Y via a map.

PImage img;
float increment = 5;
float increment2 = 150;

void setup() {
  size(700, 700, P3D);
  imageMode(CENTER);
  img = loadImage("god.jpg");
  rectMode(CENTER);
}

void draw() {
  noStroke();
  background(0);
  translate(50, 50);
  rotateY(0);
  
 // pushStyle();
 // imageMode(CENTER);
 // PImage section= img.get (mouseX,mouseY,50,50);
 // image(section, mouseX,mouseY, 200, 200);
 // //translate(50,50);
  
 //popStyle();

  for (int y = 0; y<img.height; y+= increment) {           
    for (int x = 0; x <img.width; x+= increment) { 
      img.loadPixels();
int index = (x + y * img.width);
      color pix = img.pixels[index];
      fill(pix); 
      float diam = map(brightness(pix), 0, 255, 2, increment2/2);
      pushMatrix();
      translate(x, y, diam);
     float a = map (mouseX, 0,700, 0, 20);
     float b = map (mouseY, 0,700, 0, 20);
      box(increment, increment, map(a, 0, b, 0, increment2));
       
      popMatrix();
    
    }
  }
  
  
 img.updatePixels();
 
}

 

Class Exercise: Pixels

Using code and some of the new/old functions we learned on Processing, I fiddled around with the values to change the pixels and their movement in a certain image.

PImage img;
int cellSize=2;
int columns, rows;
void setup() {
  size(512, 512, P3D);
  img = loadImage("Elizabeth Taylor.jpeg");
  img.resize(width,height);
  noStroke();
  columns=width/cellSize;
  rows=height/cellSize;
}

void draw() {
  background(255);
  img.loadPixels();
  for (int i=0; i<columns; i++) {
    for (int j=0; j<rows; j++) {
      int x= i*cellSize+cellSize/4;
      int y= j*cellSize+cellSize/2;
      int loc = x+(y*height);
      color pix=img.pixels[loc];
      float z=map(brightness(pix), 10, 255, 0, mouseX);
      float t=map(brightness(pix),255,0,255,mouseY);
      pushMatrix();
      translate(x,y,z);
      fill(pix);
      rectMode(CENTER);
      ellipse(10,0,cellSize,cellSize);
      popMatrix();
    }
  }
  img.updatePixels();
}

 

Exercise

  • Make it interactive with the mouse, the further you move the mouse in one direction the more it explodes, or goes back to it’s original position
  • Take an image and draw it with rectangles or circles with the proper colors

Hope you all get the reference (watch video with sound) Here is what I got using this code:

PImage dog;
float increment =4;
float incrementZ = 500;

void setup(){
  size(700, 500, P3D);
  dog = loadImage("C:/Users/mab1312/desktop/dog.jpg");
  rectMode(CENTER);
}
void draw(){
  noStroke();
  background(0);
  rotateY(.2);
  for (int y = 0; y<dog.height; y+= increment){
    for (int x = 0; x<dog.width; x+= increment){
      dog.loadPixels();
      int i = (x+y*dog.width);
      color pix = dog.pixels[i];
      fill(pix);
      float d = map(brightness(pix),0,255,2,incrementZ/2);
      pushMatrix();
      translate(x,y,d);
      box(increment, increment, map(mouseX, 0, width, 0, incrementZ));
      popMatrix();
    }
  }
  dog.updatePixels();
  increment = map(mouseY,0,width,4,30);
}

In Class

In class, I played around with the 3D effect on processing. I also worked on a sketch that changed the brightness of the image as it zoomed into the pixels; however, I didn’t save it correctly so can’t find the record :(.

In-Class Exercise on Pixels

<Artistic Collage>

An artistic collage intake on the fruits image using the get() function with different tint appliance at different positions.

Source Code

PImage fruits;

void setup() {
  size(512, 512);
  fruits = loadImage("fruits.png");
}

void draw() {
  noStroke();
  background(255);
  pushStyle();
   tint(255, 100, 100);
   image(fruits, 150, 100);
  popStyle();
  PImage sectionOne = fruits.get(0, 0, 200, 200);
  PImage sectionTwo = fruits.get(200, 0, 300, 200);
  tint(0, 100, 100);
  image(sectionOne, 30, 40);
  tint(200, 0, 200);
  image(sectionTwo, 50, 250);
}

 

<Playing With Pixels With Two Images>

A merge of pixels from the fruits image on to the baboon (or mandril), fixed on the pixels with R-value above 210 – mainly targeting the nose color!

Source Code

PImage baboon;
PImage fruits;

void setup() {
  size(512, 512);
  baboon = loadImage("baboon.png");
  fruits = loadImage("fruits.png");
}

void draw() {
  background(255);
  baboon.loadPixels();
  fruits.loadPixels();
  
  for (int y=0; y<height; y++) {
    for (int x=0; x<width; x++) {
      int index = x + y * width;
      float r = red(baboon.pixels[index]);
      if ( r >= 210 ) {
        baboon.pixels[index] = fruits.pixels[index];
      }
    }
  }
  
   baboon.updatePixels();
   image(baboon, 0, 0);
}

 

<Exploding Pixels>

Trying to imitate an exploding facebook logo!

Source Code

PImage facebook;
int increment = 20;

void setup() {
  size(512, 512, P3D);
  facebook = loadImage("facebook.png");
}

void draw() {
  noStroke();
  background(0);
  facebook.resize(0, 512);
  facebook.loadPixels();
  
  for (int y=0; y<facebook.height; y+= increment) {
    for (int x=0; x<facebook.width; x+= increment) {
      int index = x + y * facebook.width;
      color pix = facebook.pixels[index];
      fill(pix);
      float z = map(brightness(pix), 0, 255, 0, mouseX);
      pushMatrix();
        translate(x,y,z);
        fill(pix);
        rectMode(CENTER);
        rect(0,0,increment,increment);
      popMatrix();
    }
  }
}

 

In-Class Example

I was trying to work on this example:

  • Take an image and draw it with rectangles with the proper colors
    • Make those rects explode along the Z-axis based on the brightness of each pixel ( you need to set P3D in the size statement at the beginning of the sketch, i.e. size(512,512,P3D) )
    • Make it interactive with the mouse, the further you move the mouse in one direction the more it explodes, or goes back to it’s original position.
Here’s an attempt:

And here’s a little “bleeding effect” I made by tweaking the explode3d example:

 

Here’s the code:

PImage source;
int increment=10;
void setup() {
  size(500, 402);
  source = loadImage("godiva.jpg");
  noStroke();
}

void draw() {
  background(0);
  source.loadPixels();
  for (int y=0; y<height; y+=increment) {
    for (int x=0; x<width; x+=increment) {
      int loc = x+(y*width);
      color pix=source.pixels[loc];
      float dims=brightness(pix);
      dims=increment*(dims/205);
      fill(pix);
      rect(x,y,dims,dims);
    }
  }
  source.updatePixels();
  increment=(int)map(mouseX,0,width,5, 20);
}
PImage source;
int cellSize=2;
int columns, rows;
int inrement=10;
//int mouseX= 0;
//int mouseY= 0;
//boolean makeBW = true;
void setup() {
  size(500, 402, P3D);
  source = loadImage("godiva.jpg");
  noStroke();
  columns=width/cellSize;
  rows=height/cellSize;
}

void draw() {
  background(0);
  source.loadPixels();
  for (int i=0; i<columns; i++) {
    for (int j=0; j<rows; j++) {
      int x= i*cellSize+cellSize/2;
      int y= j*cellSize+cellSize/2;
      int loc = x+(y*width);
      color pix=source.pixels[loc];
      //float z=map(brightness(pix), 0, 255, 0, mouseX);
      pushMatrix();
      //translate(x,y,z);
      fill(pix);
      rectMode(CENTER);
      float dims=brightness(pix);
      dims=increment*(dims/255);
      rect(x,  y,dims,dims);
      popMatrix();
    }
  }
  source.updatePixels();
   increment=(int)map(mouseX,0,width,5,50);
}

  //void mousePressed(){
  
  //  for (int i =0; i<columns.length();i++){
    
  //   pixels[i].mouseX = random(-5,5);
  //   pixels[i].mouseY = random(-5,5);

  //  }
  //}

 

 

In Class Exercise

I tried experimenting with the third dimension, and this is what (with Jack’s little help) came to life.

PImage possum;
float increment;
float z;


void setup() {
  size(700, 393, P3D);
  possum = loadImage("possum.jpg");
  increment = 7;
  noStroke();
}

void draw() {
  background(0); 
  possum.loadPixels();

  for (int y=0; y<possum.height; y+=increment) {
    for (int x=0; x<possum.width; x+= increment) {
      int index = x + y * possum.width;
      color pix = possum.pixels[index];
      float diam = map(brightness(pix), 0, 255, 1, 3)*(map(mouseX, 0, width, 10, 30));
      pushMatrix();
      translate(x, y, diam);
      fill(pix);
      box(diam/2);
      popMatrix();
    }
  }

  possum.updatePixels();
}

 

In Class Exercise

For the in-class exercise, I decided to build up upon what we did during class with the pixels and played around with some of the variables. I had my own image from Thanos and first changed the ellipses to rectangles. Then, in the “pix” variable, I wanted to add a random integer from 0 to 99 to add to the color of pix in order to have a “bling” effect where the image would constantly sparkle like it is a GIF. I knew this was possible since the draw function acts like a loop, and the random values will constantly add differently every time the loop goes on.

I also played around with the constrain of increment, but one question I had in mind was “how to reverse the constrain?”, as in how do I make it so that the image zooms in the more I go left rather than right, the more I go up rather than down, etc. Because when I just reversed the values in the constrain function (1,990) to (990,1), the image would not zoom out. So maybe if I search online a bit more I will understand why this is happening.

Here is how the whole thing works, and before that is the code. Note that the main function is provided by Prof. Aaron Sherwood.

PImage a;
int x =0;
int y= 0;
int increment = 10;
void setup(){
  
  background(255);
  size(990,600);
  a=loadImage("jeff2.jpg");
  
  
}

void draw(){
  
  
  background(0);
  
  a.loadPixels();
  for (int y=0; y<height; y+=increment) {
    for (int x=0; x<width; x+=increment) {
      int loc = x+(y*width);
      color pix=a.pixels[loc]+int(random(100));
      fill(pix);
      rect(x,y,increment,increment);
    }
  }
  a.updatePixels();
  increment=constrain(mouseY,1,600);
  
  
  
  
  
  
}