在 Python 模式下从 Processing 导出 gif

Export a gif from Processing in Python mode

我正在使用 Processing in Python mode 制作一个小动画,我想将其导出为 gif。

如果我在 javascript 中编码,我可以使用 gifAnimation,但我正在使用 python 模式,我似乎无法使其工作。 我可以使用 PIL 或 Pillow,但这基本上需要编写另一个程序。

有更简单的方法吗?

只是要清楚处理 Python 模式的库:

  • 您可以使用“纯 Python”库;
  • 您不能使用 Python 带有 C 扩展等的库(例如 numpy);
  • 您不能使用 Java脚本(或 p5js)库;
  • 您可以使用 Java & Processing Java 模式库!

Art Simon 在此处有一个很好的 gifAnimation 导出示例:https://github.com/APCSPrinciples/AnimatedGIF/

循序渐进的方法:

  1. 从以下位置下载 gifAnimation 库: https://github.com/extrapixel/gif-animation/archive/3.0.zip

  2. 解压 gifAnimation 文件夹并将其复制到您的库文件夹中,如下所示: user/sketchbook/libraries/gifAnimation (Linux) 或 user/Documents/Processing/libraries/gifAnimation (Mac/Windows)

  3. 重新启动 IDE 并在草图的开头添加:

add_library('gifAnimation')
  1. setup()
  2. 中初始化导出器对象
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. draw() 结束时使用 .addFrame() 方法,也许还有一些用于动画录制结束的方法。
def draw():    
    # your drawing here

    exporter.addFrame()

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