Export png after a random time or frame count.

Hello! I use node to tweet png files from Processing. All is well but I'd like to develop this idea with your help please. Within my Processing sketches, get Processing to output my png file at any random frame count between, for example 100 and 200 frames. Not sure if there is another way to do this using a random timer?. I have an sample below that outputs a png at frame 100 only. Which is a bit static after a few iterations. Any help would be really great guys!

if (frameCount == 110) { save("output.png"); exit(); }

Answers

  • edited January 2017

    An example with millis(), but could be easy used with framecount instead.

    // initial export at 2s
    int nextExport = 2000;
    
    void setup(){}
    
    
    void draw(){
      // when sheduled time for export is reached
      if(millis() >= nextExport){
        // you could save a file now
    
        // shedule next export randomly between 1-3s
        nextExport += random(1000, 3000);
        println("next export at: "+nextExport);
      }
    }
    

    But you do not really want to post images on twitter every few seconds, right?

  • Thanks for the reply. I just want to export the png after a random amount of time passed on the animation. Posting on twitter is not in scope as this is done using node.

  • edited January 2017

    Here's an example using frameCount:

    `

    ArrayList<Brush> brushes; 
    
    void setup() {
      size(640, 640);
      background(255);
      brushes = new ArrayList<Brush>();
      for (int i = 0; i < 50; i++) {
        brushes.add(new Brush());
      }
    }
    
    void draw() {
      for (Brush brush : brushes) {
        brush.paint();
    
      if (frameCount == 100) {
       save("output.png");
       exit();
      // save("output.png");
      // exit();
      }
    
    
      }
    }
    
    void mouseClicked() {
      setup();
    }
    
    class Brush {
      float angle;
      int components[];
      float x, y;
      color clr;
    
      Brush() {
        angle = random(TWO_PI);
        x = random(width);
        y = random(height);
        clr = color(random(255), random(255), random(255), 5);
        components = new int[2];
        for (int i = 0; i < 2; i++) {
          components[i] = int(random(1, 5));
        }
      }
    
      void paint() {
        float a = 0;
        float r = 0;
        float x1 = x;
        float y1 = y;
        float u = random(0.5, 1);
    
        fill(clr);
        noStroke();    
    
        beginShape();
        while (a < TWO_PI) {
          vertex(x1, y1); 
          float v = random(0.85, 1);
          x1 = x + r * cos(angle + a) * u * v;
          y1 = y + r * sin(angle + a) * u * v;
          a += PI / 180;
          for (int i = 0; i < 2; i++) {
            r += sin(a * components[i]);
          }
        }
        endShape(CLOSE);
    
        if (x < 0 || x > width ||y < 0 || y > height) {
          angle += HALF_PI;
        }
    
        x += 2 * cos(angle);
        y += 2 * sin(angle); 
        angle += random(-0.15, 0.15);
    
      }
    }
    

    `

  • Answer ✓

    Well instead of your current version with a fixed value of 100 frames, you could use a variable for that. Ans in setup() you can assign a random value to it.

    int numFrames;
    
    void setup(){
      // random number between 50 and 300
      numFrames = (int)random(50, 300);
    }
    
    void draw(){
      if(frameCount == numFrames){
        // export...
      }
    }
    
  • Answer ✓

    And assign a new random value every time the condition is met

  • That's just perfect guys, thank you so much for taking time out over the weekend to help me. Cheers!

Sign In or Register to comment.