# Cant get sequence of images to save

**URL:** https://discourse.processing.org/t/cant-get-sequence-of-images-to-save/24630
**Category:** Coding Questions
**Created:** [October 16, 2020, 1:23am UTC](https://discourse.processing.org/t/cant-get-sequence-of-images-to-save/24630 "2020-10-16T01:23:50Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![DeadMusik](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/deadmusik/32/10863_2.png) [@DeadMusik](https://discourse.processing.org/u/DeadMusik)
#### Post date: [October 16, 2020, 1:23am UTC](https://discourse.processing.org/t/cant-get-sequence-of-images-to-save/24630/1 "2020-10-16T01:23:50Z")

</div>

Hello, so I picked this code up off the net, its not mine but after doing some tweaks I was trying to get it to save images so I could upload into after effects, can you please tell me where I need to put my if statement for when I am recording, I have tried many places to no aval. thank you in advance!!!

```auto
ParticleSystem ps;
FlowField ff;

float t = 0;
boolean pause = false;
boolean recording = false;

color black = color(0, 0, 0);
color black2 = color(0, 0, 0);

void setup()
{
  fullScreen();
  //size(720, 480);
  
  bg(black, black2);  
  
  ff = new FlowField();
   
  ps = new ParticleSystem();
  ps.addParticles();  
}

void draw()

{ 
  ff.init(t);
  ps.run(ff);
  t += 0.0003;
}

void mousePressed()
{
  noiseSeed((int)random(10000));
  ps.restart();
  bg(black, black2);
  pause = false;
}

void keyPressed()
{
  if (key == 'p' || key == 'P') pause = !pause;
  if (pause) { noLoop(); }
  else { loop(); }
  
  if (key == 's' || key == 'S') saveFrame("output_1/smoke-###.png");
  
  if (key == 'r' || key == 'R') recording = !recording; 
  if (recording) { loop();
  saveFrame("Recording_1/Particle_Perlin-###.png");
  }
  else { noLoop(); }
}

void bg(color c1, color c2)
{
  for (int i = 0; i < height; i++)
  {
    float j = map(i, 0, height, 0, 1);
    stroke(lerpColor(c1,c2,j*300));
    line(0, i, width, i);
  }
}
class FlowField
{
  PVector[][] field;
  int cols, rows;
  int resolution;
  
  FlowField()
  {
    resolution = 10;
    cols = width/resolution;
    rows = height/resolution;
    field = new PVector[cols][rows];
  }
  
  void init(float t)
  {
    float xoff = 0;
    for (int i = 0; i < cols; i++)
    {
      float yoff = 0;
      for (int j = 0; j < rows; j++)
      {
        float theta = noise(xoff, yoff, t);
        theta = map(theta, 0, 1, 0, TWO_PI);
        
        field[i][j] = new PVector(cos(theta), sin(theta));
        
        yoff += 0.07;
      }
      xoff += 0.07;
    }
  }

  
  PVector lookup(PVector lookup)
  {
    int column = int(lookup.x / resolution);
    column = constrain(column, 0, cols - 1);
    
    int row = int(lookup.y / resolution);
    row = constrain(row, 0, rows - 1);
    
    return field[column][row].get();
  }
  
  
}
class Particle
{
  PVector location;
  PVector velocity;
  PVector acceleration;
  float maxSpeed;
  float maxForce;
  
  
  Particle(PVector l)
  {
    location = l.get();
    velocity = new PVector(0, 0);
    acceleration = new PVector(0, 0);
    maxSpeed = 100;
    maxForce = 0.15;
  }
    
    
  void bounderies()
  {
    if (location.x < 0) { location.x = width; }
    else if (location.x > width) { location.x = 0; }
    
    if (location.y < 0) { location.y = height; }
    else if (location.y > height) { location.y = 0; }    
  }
  
  
  void follow(FlowField flow)
  {
    PVector desired = flow.lookup(location);
    desired.limit(maxSpeed);
    
    PVector steer = PVector.sub(desired, velocity);
    steer.limit(maxForce);
    applyForce(steer);
  }
  
  
  void applyForce(PVector force)
  {
    acceleration.add(force);
  }
  
  
  void update()
  {
    velocity.add(acceleration);
    velocity.limit(maxSpeed);
    location.add(velocity);
    acceleration.mult(0);
  }
  
  
  void display()
  {
    stroke(chooseColor(), 10);
    point(location.x, location.y);
  
  }
  
  
  color chooseColor()
  {
    color c;
    color orange = color(255, 246, 237);
    color white = color(255, 255, 255);
    color blue = color(163, 229, 253);
    
    int choice = frameCount % 400;
    int amt = (frameCount % 100) / 100;
    
    if (choice < 100) { c = lerpColor(orange, white, amt); }
    else if (choice < 200) { c = lerpColor(white, blue, amt); }
    else if (choice < 300) { c = lerpColor(blue, white, amt); }
    else { c = lerpColor(white, orange, amt); }
    
    return c;
  }
}
class ParticleSystem
{
  ArrayList <Particle> particles;
  
  
  ParticleSystem()
  {
    particles = new ArrayList <Particle> ();
  }
  
  
  void addParticles()
  {
    for (int i = 0; i < 10000; i++)
    {
      PVector l = new PVector(random(width), random(height));
      particles.add(new Particle(l));
    }
  }
  
  
  void restart()
  {
    for (int i = 99; i >= 0; i--) { particles.remove(i); }
    addParticles();
  }
  
  
  void run(FlowField flow)
  {
    for (Particle p : particles)
    {
      p.follow(flow);
      p.bounderies();       
      p.update();
      p.display();
    }
  }

}

```

---

<div class="post-metadata">

### Author: ![matheplica](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/matheplica/32/21035_2.png) [@matheplica](https://discourse.processing.org/u/matheplica)
#### Post date: [October 16, 2020, 8:25am UTC](https://discourse.processing.org/t/cant-get-sequence-of-images-to-save/24630/2 "2020-10-16T08:25:10Z")

</div>

You should had saveFrame at the end of your draw function

```auto
void draw(){
  ff.init(t);
  ps.run(ff);
  t += 0.0003;
  if (recording) saveFrame("Recording_1/Particle_Perlin-###.png");
  }

```

---

<div class="post-metadata">

### Author: ![PutridGospel](https://avatars.discourse-cdn.com/v4/letter/p/ec9cab/32.png) [@PutridGospel](https://discourse.processing.org/u/PutridGospel)
#### Post date: [October 16, 2020, 1:56pm UTC](https://discourse.processing.org/t/cant-get-sequence-of-images-to-save/24630/4 "2020-10-16T13:56:14Z")

</div>

I tried that location and I was still getting errors there and it was saying that the `void mousePressed()` was wrong.

---

<div class="post-metadata">

### Author: ![DeadMusik](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/deadmusik/32/10863_2.png) [@DeadMusik](https://discourse.processing.org/u/DeadMusik)
#### Post date: [October 16, 2020, 7:06pm UTC](https://discourse.processing.org/t/cant-get-sequence-of-images-to-save/24630/5 "2020-10-16T19:06:57Z")

</div>

Hey @matheplica I have two accounts sorry, Run this code and tell me what you get, it will slow down (usual for my computer when saving and rendering) but then it will only save a frame then continue (ONLY) rendering. so sorry for my explanation but it seems like my save function is outside the draw loop somehow… when placed after, anyways run this lmk please… thanks in advance

```auto
ParticleSystem ps;
FlowField ff;

float t = 0;
boolean pause = false;
boolean recording = false;

color black = color(0, 0, 0);
color black2 = color(0, 0, 0);

void setup()
{
  fullScreen();
  //size(720, 480);
  
  bg(black, black2);  
  
  ff = new FlowField();
   
  ps = new ParticleSystem();
  ps.addParticles();  
}

void draw()

{ 
  ff.init(t);
  ps.run(ff);
  t += 0.0003;
  if (recording) saveFrame("Recording_1/Particle_Perlin-########.png");
}

void mousePressed()
{
  noiseSeed((int)random(10000));
  ps.restart();
  bg(black, black2);
  pause = false;
}

void keyPressed()
{
  if (key == 'p' || key == 'P') pause = !pause;
  if (pause) { noLoop(); }
  else { loop(); }
  
  if (key == 's' || key == 'S') saveFrame("output_1/smoke-###.png");
  
  if (key == 'r' || key == 'R') recording = !recording; 
  
}

void bg(color c1, color c2)
{
  for (int i = 0; i < height; i++)
  {
    float j = map(i, 0, height, 0, 1);
    stroke(lerpColor(c1,c2,j*300));
    line(0, i, width, i);
  }
}
class FlowField
{
  PVector[][] field;
  int cols, rows;
  int resolution;
  
  FlowField()
  {
    resolution = 10;
    cols = width/resolution;
    rows = height/resolution;
    field = new PVector[cols][rows];
  }
  
  void init(float t)
  {
    float xoff = 0;
    for (int i = 0; i < cols; i++)
    {
      float yoff = 0;
      for (int j = 0; j < rows; j++)
      {
        float theta = noise(xoff, yoff, t);
        theta = map(theta, 0, 1, 0, TWO_PI);
        
        field[i][j] = new PVector(cos(theta), sin(theta));
        
        yoff += 0.07;
      }
      xoff += 0.07;
    }
  }

  
  PVector lookup(PVector lookup)
  {
    int column = int(lookup.x / resolution);
    column = constrain(column, 0, cols - 1);
    
    int row = int(lookup.y / resolution);
    row = constrain(row, 0, rows - 1);
    
    return field[column][row].get();
  }
  
  
}
class Particle
{
  PVector location;
  PVector velocity;
  PVector acceleration;
  float maxSpeed;
  float maxForce;
  
  
  Particle(PVector l)
  {
    location = l.get();
    velocity = new PVector(0, 0);
    acceleration = new PVector(0, 0);
    maxSpeed = 100;
    maxForce = 0.15;
  }
    
    
  void bounderies()
  {
    if (location.x < 0) { location.x = width; }
    else if (location.x > width) { location.x = 0; }
    
    if (location.y < 0) { location.y = height; }
    else if (location.y > height) { location.y = 0; }    
  }
  
  
  void follow(FlowField flow)
  {
    PVector desired = flow.lookup(location);
    desired.limit(maxSpeed);
    
    PVector steer = PVector.sub(desired, velocity);
    steer.limit(maxForce);
    applyForce(steer);
  }
  
  
  void applyForce(PVector force)
  {
    acceleration.add(force);
  }
  
  
  void update()
  {
    velocity.add(acceleration);
    velocity.limit(maxSpeed);
    location.add(velocity);
    acceleration.mult(0);
  }
  
  
  void display()
  {
    stroke(chooseColor(), 10);
    point(location.x, location.y);
  
  }
  
  
  color chooseColor()
  {
    color c;
    color orange = color(255, 246, 237);
    color white = color(255, 255, 255);
    color blue = color(163, 229, 253);
    
    int choice = frameCount % 400;
    int amt = (frameCount % 100) / 100;
    
    if (choice < 100) { c = lerpColor(orange, white, amt); }
    else if (choice < 200) { c = lerpColor(white, blue, amt); }
    else if (choice < 300) { c = lerpColor(blue, white, amt); }
    else { c = lerpColor(white, orange, amt); }
    
    return c;
  }
}
class ParticleSystem
{
  ArrayList <Particle> particles;
  
  
  ParticleSystem()
  {
    particles = new ArrayList <Particle> ();
  }
  
  
  void addParticles()
  {
    for (int i = 0; i < 10000; i++)
    {
      PVector l = new PVector(random(width), random(height));
      particles.add(new Particle(l));
    }
  }
  
  
  void restart()
  {
    for (int i = 99; i >= 0; i--) { particles.remove(i); }
    addParticles();
  }
  
  
  void run(FlowField flow)
  {
    for (Particle p : particles)
    {
      p.follow(flow);
      p.bounderies();       
      p.update();
      p.display();
    }
  }

}

```

---

<div class="post-metadata">

### Author: ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)
#### Post date: [October 17, 2020, 3:52pm UTC](https://discourse.processing.org/t/cant-get-sequence-of-images-to-save/24630/6 "2020-10-17T15:52:48Z")

</div>

It’s also boundaries

---

<div class="post-metadata">

### Author: ![PutridGospel](https://avatars.discourse-cdn.com/v4/letter/p/ec9cab/32.png) [@PutridGospel](https://discourse.processing.org/u/PutridGospel)
#### Post date: [October 19, 2020, 12:12pm UTC](https://discourse.processing.org/t/cant-get-sequence-of-images-to-save/24630/7 "2020-10-19T12:12:53Z")

</div>

Can I get a little bit of an explanation pertaining to my code, and I will try to get this worked out Im doing visuals for a halloween party and really want to get this recorded so I can do some mograph effects, thank you

---

<div class="post-metadata">

### Author: ![DeadMusik](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/deadmusik/32/10863_2.png) [@DeadMusik](https://discourse.processing.org/u/DeadMusik)
#### Post date: [October 28, 2020, 3:26am UTC](https://discourse.processing.org/t/cant-get-sequence-of-images-to-save/24630/8 "2020-10-28T03:26:41Z")

</div>

Hey @Chrisir could I get a little more of an explanation in my code i found the function for boundaries, should i tweak that section to accept the saving of files. I’m probably making no sense, my thought process is vague still.

---

<div class="post-metadata">

### Author: ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)
#### Post date: [October 28, 2020, 4:26am UTC](https://discourse.processing.org/t/cant-get-sequence-of-images-to-save/24630/9 "2020-10-28T04:26:07Z")

</div>

I think I was just making a lame remark about a typo

Apologies
