how to make a random image loop inside of an ellipse ?

edited March 2015 in How To...

Hello dear people! As many others, I'm also a beginner with Processing willing to execute certain idea and feeling helpless with that. The thing is following :

  1. I want to create a loop constantly displaying random images from a folder, one by one, one image on top of another.

  2. All of them should be displayed inside of one unchanged ellipse.

  3. The constant reappearance of new images should be stopped when mouse is clicked. For that moment you would see the last image that appeared before you clicked. When you stop pressing the mouse button, the random image display would continue.

Here is what I've found to make the mask for an ellipse, but that is just one checkpoint. I still have no idea how to create that 'image lottery'.

        PGraphics mask;
        PImage img;    

        void setup() {    
          img=loadImage("0.jpg");    
          img.resize(1000,0);    
            size(img.width,img.height);    
           background(40);    
          mask=createGraphics(width,height);//draw the mask object    
          mask.beginDraw();    
          mask.background(0);//background color to target    
          mask.fill(255);    
          mask.ellipse(width/2,height/2,500,500);    
          mask.endDraw();      
          img.mask(mask);     
         image(img,0,0);
        }

I will love you endlessly for your help!

Answers

  • Oh thanks! Now it is better, right ? But still, do you have any idea about the project I'm willing to make ?

  • Answer ✓

    simple draft

    //
    //states 
    final int stateWaitForFolder = 0;  // consts  
    final int stateReadImages    = 1;
    final int stateMain          = 2;
    int state = stateWaitForFolder;  // current
    
    String folderGlobal = "";  
    String[] strList; 
    PImage[] imgList;
    
    int maxImages=0;
    int i2 = 0; 
    boolean imagesGoOn;
    
    void setup() {
      size (1100, 700);
      background(111);
    
      File f = new File(dataPath(""));
      selectFolder("Select a folder to process:", "folderSelected", f);
    }
    
    void draw() {
      // 
      switch (state) {
    
      case stateWaitForFolder:
    
        // waiting until folder has been selected 
        if (!folderGlobal.equals("")) {
          println (folderGlobal);
          File f = dataFile(folderGlobal);
          strList = f.list();
          println ("found " + strList.length);
          imgList = new PImage [ strList.length ];
          int i = 0;
          for (String imageName : strList) {
            if (imageName.indexOf(".JPG")>-1 ||
              imageName.indexOf(".jpg")>-1) {
              println(imageName);
              imgList[i] = loadImage (folderGlobal+"/"+imageName);
              // image (imgList[i], i*60, 0);
              maxImages=i;
              i++;
            } // if
          } // for 
          frameRate(2);
          imagesGoOn=true;
          if (maxImages>0) {
            state = stateReadImages; //  next state
          } else
          {
            println ("None found.");
            folderGlobal="";
            f = new File(dataPath(""));
            selectFolder("Select a folder to process:", 
            "folderSelected", f);
          }
        } // if 
        break;
    
      case stateReadImages:    
        if (imagesGoOn) {
          background(111);
          image(imgList[i2], 100, 110);
          i2++;    
          if (i2>maxImages) 
            i2=0;
        } else 
        {
          text ("Stop", 200, 200);
        }
        // state = stateMain; //  next 
        break;
    
      case stateMain:
        // 
        break;
    
      default:
        // 
        println ("error 91"); 
        exit();
        break;
        //
      }  // switch
      //
    }  // func 
    
    // -----------------------------------------
    // Inputs 
    
    void mousePressed() {
      imagesGoOn=!imagesGoOn;
    }
    
    // ------------------------------------------
    // file handling 
    
    void folderSelected(File selection) {
      if (selection == null) {
        println("Window was closed or the user hit cancel.");
      } else {
        println("User selected " + selection.getAbsolutePath());
        folderGlobal = selection.getAbsolutePath();
      } // else
    }
    //
    
  • edited March 2015

    Hey! Thanks Chrisir! It runs quite nice. I tried to figure out what makes what in the code and adjust it a bit more for what I wanted. But I didn't succeed I must admit :-<

    For example how can I put the images in that ellipse mask (so that they all appear inside of a circular frame in the center of the screen )? I centered them for now, but no idea how to create that ellipse mask. I wanted to apply the mask code I already posted above, but it said Cannot invoke mask (PGraphics) on the array type PImage[] _ .

    Another thing is that the images display in their normal order from the folder and I wanted to make them come out at random order.

  • edited March 2015

    Cannot invoke mask (PGraphics) on the array type PImage[]

  • yes, as gotoloop said

     img.mask(mask);    
     image(img,0,0);
    

    becomes

     imgList[i2].mask(mask);    
     image(imgList[i2],0,0);
    
  • for random order please just use

    i2 = random(maxImages);

  • Hey Chrisir! Where should that line for random be placed ?

  • after line 65 pls

  • btw you can get rid of stateMain

    stateReadImages can be called stateShowImages

Sign In or Register to comment.