Is there a way to save turtle's drawing as an animated GIF?

不羁的心 提交于 2019-11-28 12:58:30
cdlane

Make an animated GIF from Python turtle using Preview on OSX

1) Start with a working program

As obvious as that seems, don't be debugging your code while trying to generate the animated GIF. It should be a proper turtle program with no infinite loops that ends with mainloop(), done(), or exitonclick().

The program I'm going to use for this explanation is one I wrote for Programming Puzzles & Golf Code that draws an Icelandic flag using turtle. It's intentionally minimalist as it is PP&GC:

from turtle import *
import tkinter as _
_.ROUND = _.BUTT
S = 8
h = 18 * S
color("navy")
width(h)
fd(25 * S)
color("white")
width(4 * S)
home()
pu()
goto(9 * S, -9 * S)
lt(90)
pd()
fd(h)
color("#d72828")
width(S + S)
bk(h)
pu()
home()
pd()
fd(25 * S)
ht()
done()

2) Have your program save snapshots on a timed basis

Repackage your program with draw(), save() and stop() timed events roughly as follows:

from turtle import *
import tkinter as _
_.ROUND=_.BUTT

def draw():
    S = 8
    h = 18 * S
    color("navy")
    width(h)
    fd(25 * S)
    color("white")
    width(4 * S)
    home()
    pu()
    goto(9 * S, -9 * S)
    lt(90)
    pd()
    fd(h)
    color("#d72828")
    width(S + S)
    bk(h)
    pu()
    home()
    pd()
    fd(25 * S)
    ht()

    ontimer(stop, 500)  # stop the recording (1/2 second trailer)

running = True
FRAMES_PER_SECOND = 10

def stop():
    global running

    running = False

def save(counter=[1]):
    getcanvas().postscript(file = "iceland{0:03d}.eps".format(counter[0]))
    counter[0] += 1
    if running:
        ontimer(save, int(1000 / FRAMES_PER_SECOND))

save()  # start the recording

ontimer(draw, 500)  # start the program (1/2 second leader)

done()

I'm using 10 frames per second (FPS) as that will match what Preview uses in a later step.

3) Run your program; quit after it completes.

Create a new, empty directory and run it from there. If all goes to plan, it should dump a series of *.eps files into the directory.

4) Load all these *.eps files into Preview

Assuming Preview is my default previewer, in Terminal.app I would simply do:

open iceland*.eps

5) Select-All the PDF (were EPS) files in the Preview sidebar and File/Export... (not Export as PDF) as GIF

Set the export type under the Options button, save them into our temporary directory. You need to hold down the Option key when selecting a format to see the GIF choice. Pick a good screen resolution. We should now have *.gif files in our temporary directory. Quit Preview.

6) Load all the *.gif files into Preview

open iceland*.gif

7) Merge all but first GIF file into the first GIF file

Select All the GIF files in Preview's sidebar. Unselect (Command Click) the first GIF file, e.g. iceland001.gif. Drag the selected GIF files onto the unselected GIF file. This will modify it and it's name. Use File/Export... to export the modified first GIF file to a new GIF file, e.g. iceland.gif

8) This is an animated GIF!

Convince yourself by loading it into Safari, e.g.:

open -a Safari iceland.gif

9) Converting to a repeating animated GIF

For a repeating animated GIF, you'll need some external tool like ImageMagick or Gifsicle to set the loop value:

convert -loop 0 iceland.gif iceland-repeating.gif

And again convince yourself that it works:

open -a Safari iceland-repeating.gif

10) Animated GIF result. Good luck!

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!