Using gifAnimation library on Python Mode

I have answered this today on Stack Overflow and I think it might be useful for someone:

Just to be clear about libraries with Processing Python mode:

  • You can use “pure Python” libraries;
  • You can’t use Python libraries with C extensions & etc (like numpy);
  • You can’t use JavaScript (or p5js) libraries;
  • You can use Java & Processing Java mode libraries (most of them work)!

Art Simon has a nice example of gifAnimation export here: https://github.com/APCSPrinciples/AnimatedGIF/

A step by step approach:

  1. Download the gifAnimation library from:
    https://github.com/extrapixel/gif-animation/archive/3.0.zip

  2. Unzip and copy the gifAnimation folder into your libraries folder, like this:
    user/sketchbook/libraries/gifAnimation (Linux) or
    user/Documents/Processing/libraries/gifAnimation (Mac/Windows)

  3. Restart the IDE and add this at the start of your sketch:

add_library('gifAnimation')
  1. Initialize an exporter object inside setup()
def setup():
    global exporter
    size(400, 400)
    exporter = GifMaker(this, "animation.gif")
    exporter.setRepeat(0)     # infinite repeat
    exporter.setQuality(100)  # test values
    exporter.setDelay(200)    # milliseconds 
  1. at the end of draw() use the .addFrame() method and maybe something for the end of the animation recording.
def draw():    
    # your drawing here

    exporter.addFrame()

    if keyPressed and key == 'e':
        exporter.finish()
        print("gif saved, exit")
        exit()
4 Likes

Adding an extra warning to ensure you rename the unzipped Folder from /gif-animation-3.0 to /gifAnimation and a link to the thread that demonstrates how to show an animated gif instead of exporting one (another feature from this library) Loop animated .gif in python mode - #3 by JSGauthier