Best way to export a gif from oF


I’m wondering what is the best way to export/save a gif file from oF. I’m working on a series of animated visuals and would like to be able to export small gifs (< 6sec) as documentation.

At the moment, to do this, I capture part of the screen as the animation is running and then convert the resulting .mov file into .gif online. It’s super tedious and the quality of the visual/animation drops significantly, as you can see in the uploads.

I’ve looked into ofxGifEncoder but for some reason my code won’t find the main .h file.

Looking forward to hearing your advice ! Much love to the community.

Hi, I don’t know what’s “the best way”, but what I do is saving the frames to a folder, and then calling command line programs to produce a good quality gif:

The best command line program to compress the gif and reduce the color count is

So my approach is:

  1. Save frames
  2. Call ffmpeg to calculate the ideal palette
  3. Call ffmpeg to produce an animated gif
  4. Call gifsicle to optimize the file size

Another one-step approach is to use this library but it produces non-optimized gifs and it’s not very fast if I remember right.

I forked ofxGifEncoder and fixed it so it works on my OF (compiled from Git, 0.9.8).

Hi @hamoid !

Thanks so much for your replies / fork.

I downloaded ofxGifEncoder and tried it but I am running into an issue. In this simple test animation, I am trying to get the code to export a gif of 4 seconds when key ‘g’ is pressed.

I get a error ‘Expected expression’ on line :

gifEncoder.addFrame(foo.getPixels(), frameW, frameH);

That line doesn’t make sense to me.

My code in the ofApp.cpp

void ofApp::setup(){

frameW  = ofGetWidth();
frameH  = ofGetHeight();

gifEncoder.setup(frameW, frameH, 4.0, 256);
gifEncoder.addFrame(foo.getPixels(), frameW, frameH);

void ofApp::draw(){

ofColor bg_color=ofColor::fromHex(0x303030);
ofColor color1=ofColor::fromHex(0xbaba8f);


int radius=100;

ofDrawSphere(0, 0, ofGetMouseX()/2);

void ofApp::keyPressed(int key){

if (key == 'g') {"test_"+ofGetTimestampString()+".gif");
    cout << "saving gif" << endl;

Can you tell what I am missing ?


What if you replace

gifEncoder.addFrame(foo.getPixels(), frameW, frameH);


gifEncoder.addFrame(foo.getPixels().getData(), frameW, frameH);

That’s what the included example uses.

Hey @hamoid , thanks for the reply.

Tried using gifEncoder.addFrame(foo.getPixels().getData(), frameW, frameH); like you suggested but it does not work. I get this error:

Use of undeclared identifier 'foo’

I don’t understand what is ‘foo’.

My ofApp.h file :

#pragma once
#include "ofMain.h"
#include "ofxGifEncoder.h"

class ofApp : public ofBaseApp{

void setup();
void draw();

void keyPressed(int key);

int frameW, frameH;

ofxGifEncoder gifEncoder;


“foo” is used in programming languages as “bla bla”, or “whatever”. It’s supposed to be replaced by something meaningful, and it’s not yet declared in your program, that’s why it fails.

I added another example to the repo: (example-draw). It produces a pulsing circle anim gif. Cheers :slight_smile: