Code for Image Manipulation

PImage face;
float redValavg;
float greenValavg;
float blueValavg;
color buf;
int randp1;
color hold;
boolean same_face = true;
int pos_lr = 0;
int pos_ud = 0;
int dimension;
int pix_mix=2;
void setup() {
size(640, 426);
face = loadImage(“Joe_Face_2.jpg”);
}
void draw() {
image(face, pos_lr, pos_ud);
}

void keyPressed(){
if( key == ‘v’){// Exchanges Pixels Randomly
dimension = face.width * face.height;
face= sameface(face);
face.loadPixels();
for (int i = 0; i < dimension; i += pix_mix) {
randp1=round(random(0,dimension-1));
hold = face.pixels[i];
face.pixels[i] = face.pixels[randp1];
face.pixels[randp1]=hold;
}
face.updatePixels();
}
if( key == ‘t’) same_face= !same_face; //Resets to FirstState
if ( key == ‘b’){ //Adds Random Colors instead of Pixels;
face= sameface(face);
dimension = face.width * face.height;
face.loadPixels();
for (int i = 0; i < dimension; i += pix_mix) {
face.pixels[i] = color(0, 0, 0);
redValavg = random(0,255);
greenValavg= random(0,255);
blueValavg=random(0,255);
buf= color(redValavg,greenValavg,blueValavg);
if(i%2==0)face.pixels[i]=buf;
}
face.updatePixels();

}
if( key ==’c’){// Darkens
face= sameface(face);
dimension = face.width * face.height;
face.loadPixels();
for (int i = 0; i < dimension; i += pix_mix) {
face.pixels[i] = color(0, 0, 0);
}
face.updatePixels();

}
if( key ==’x’){// Brightens
face= sameface(face);
dimension = face.width * face.height;
face.loadPixels();
for (int i = 0; i < dimension; i += pix_mix) {
face.pixels[i] = color(255, 255, 255);
}
face.updatePixels();

}
//Moving the Image
if( keyCode == UP){
background(#FFFFFF);
pos_ud+=50;
}
if( keyCode == DOWN){
background(#FFFFFF);
pos_ud-=50;
}
if( keyCode == RIGHT){
background(#FFFFFF);
pos_lr+=50;
}
if( keyCode == LEFT){
background(#FFFFFF);
pos_lr-=50;
}
//pix_mix
if(key == ‘a’){
pix_mix=2*pix_mix;
println(“pixel num:” + pix_mix);
}
if(key == ‘s’){
if(pix_mix>1)pix_mix=pix_mix/2;
println(“pixel num:” + pix_mix);
}
if( key ==’m’) setup();

}

PImage sameface(PImage option){
if(same_face) return option;
return option = loadImage(“Joe_Face_2.jpg”);
}

Comments are closed.