Randomly display pieces of an image

edited December 2013 in Questions about Code

my problem is I have 2 images. I figured out how to cut the image into pieces but now I want to randomly display the pieces of both images. So I have two pictures of faces, I want the eyes to randomly switch between the two images, the nose, forehead, etc... So its like a constantly changing face... (sorry if thats confusing haha) heres the code maybe it'll make more sense ---

PImage img; 
PImage img2;


void setup(){ 
  background(0);
  size(1000,600); 
  img = loadImage("face1.jpeg");
  img2= loadImage("face2.jpeg"); 




}
void draw(){ 

 // image(img2, width/4,0);

 image(img,width/4,0); 
  PImage forehead = get(width/4,0,img.width,img.height/4); 
  PImage eyeregion = get(width/4,img.height/4,img.width,img.height/4);
  PImage noseregion = get(width/4,img.height/2,img.width,img.height/4);
  PImage mouthregion = get(width/4,img.height-img.height/4,img.width,img.height/4);
  //image(mouthregion, img.width, img.height/4); // 

int p = 1; 

switch(p) {
  case 1: 
  println("it's 1");
  image(forehead, width/4, height);
  break;

  case 2: 
  println("it's 2"); 
  image(eyeregion,width/4, 125);
  //height/4 looks pretty neat
  break;

  case 3: 
  println("it's 3"); 
  image(noseregion, width/4, img.height/2);
  break;

  case 4: 
  println("hey its 4");
   image(mouthregion,width/4, 375);
   break; 
}



} 

So I want to load the second image like this, but then randomly display the pieces of both images,

how should I go about doing this? should I use createGraphics? and like save the "regions" as separate images? the problem there is I want to add alot more images so it could get messy...

any ideas / help is much appreciated!! thanks!

Tagged:

Answers

  • Have you thought about using random()? [..]
    http://processing.org/reference/random_.html

    switch((int) random(1, 5)) {}

  • edited December 2013

    Thanks for the response! Ya I plan on using random,
    but when you run this code for example you have to show the full image first, and then get() just copies the pixels of some area. How do I create basically (I guess) a new PImage, or PGraphics?, from that.

    Because basically this wont show the foreheadregion and mouthregion images without showing the whole face, I need to take the face image and split into 4 different pictures I guess. But I'm not sure how...

  • Dunno much how to do it either. However I've got an imperfect grid picture online example:
    http://studio.processingtogether.com/sp/pad/export/ro.9$MTikWt80-9w/latest

  • Answer ✓

    get() returns a PImage, too.

    I see two possible approaches:

    • The cleanest and probably easiest is to use an image editing tool, like Gimp, to isolate the parts of the face, and to save them to PNG format with transparency around. Then you can load each part and display them as you wish.
    • Otherwise, you can compute the coordinates of each part, get() them into PImage, and you display them as above, but with more squarish look...
Sign In or Register to comment.