The most simple and modular way to export a gif

edited April 2016 in Library Questions

Hi folks!

I've created two gif-export-functions in a seperate file to simplify the process of exporting gifs. Now all i have to do is calling the gifsetup-function in the setup and the gifdraw-function in draw.

This is an example for a sketch

int a = 0;

void setup() {
  size(540, 540);

// Call the gifsetup-function
  gifsetup();
}

void draw() {
ellipse(width/2,height/2,a,a);
a++;
// Call the gifdraw-function
// Specify, how many frames shall be recorded
  gifdraw(40);
}

The functions are stored in a separate file (gifexport.pde)

import gifAnimation.*;

GifMaker gifExport;

void gifsetup() {
  println("gifAnimation " + Gif.version());
  gifExport = new GifMaker(this, "export.gif");
  gifExport.setRepeat(0);
  gifExport.setDelay(50);
}

void gifdraw(int frames) {

  gifExport.addFrame();
  if (frameCount == frames) {
    gifExport.finish();
    println("gif saved");
    exit();
  }
}

As you can see, all i have to do to export a gif is calling two functions: gifsetup(); in the setup and gifdraw(); in the draw-section.

Now my question is: Is it possible to simplify the gif-export a bit more? It would be perfect, to just have one lie of code instead of two. Maybe like this

gif(40);

Thanks and enjoy your weekend!

Tim

Answers

  • Answer ✓

    Gifdraw could call gifsetup when framecount = 1, removing the need for the line in setup()

  • edited March 2016

    Perfect! Thank you, koogs!

        int a = 0;
    
        void setup() {
          size(540, 540);
        }
    
        void draw() {
        ellipse(width/2,height/2,a,a);
        a++;
        // Call the gif-function
          gif(40);
        }
    

    gifexport.pde

    import gifAnimation.*;
    GifMaker gifExport;
    
    void gifsetup() {
      println("gifAnimation " + Gif.version());
      gifExport = new GifMaker(this, "export.gif");
      gifExport.setRepeat(0);
      gifExport.setDelay(50);
    }
    
    void gif(int frames) {
    
      if (frameCount == 1) {
       gifsetup(); 
      }
    
      gifExport.addFrame();
      if (frameCount == frames) {
        gifExport.finish();
        println("gif saved");
        exit();
      }
    }
    
  • Here's the same approach for video-export:

    Note for mac-users: ffmpeg must to be installed here, even if your processing-folder is located on a different place:

    /documents/processing/addons/ffmpeg
    

    my sketch

    int a = 0;
    
    void setup() {
      size(540, 540);
    }
    
    void draw() {
    ellipse(width/2,height/2,a,a);
    a++;
    // Call the gif-function
      video(40);
    }
    

    videoexport.pde

    import com.hamoid.*;
    
    VideoExport videoExport;
    
    
    void videosetup() {
      videoExport = new VideoExport(this, "basic.mp4");
    }
    
    void video(int frames) {
    
      if (frameCount == 1){
       videosetup(); 
      }
    
     videoExport.saveFrame();
      if (frameCount == frames) {
        exit();
      }
    }
    
  • Does this work for Processing 3?

Sign In or Register to comment.