Search code examples
pythonexportprocessinggif

Export a gif from Processing in Python mode


I'm using Processing in Python mode to make a little animation, and I would like to export it as a gif.

If I were coding in javascript, I could use gifAnimation, put I am using python mode, I can't seem to make it work. I could use PIL or Pillow, but that would basically require writing another program.

Is there an easier way to do it?


Solution

  • 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!

    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()