有没有一种方法可以将乌龟的图形另存为GIF动画? [英] Is there a way to save turtle's drawing as an animated GIF?

查看:114
本文介绍了有没有一种方法可以将乌龟的图形另存为GIF动画?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我喜欢turtle模块在Python中所做的事情,我想输出它绘制图形的整个动画.有没有办法做到这一点? GIF/MP4/任何显示动画的内容.请注意,我知道可以使用外部屏幕录像机来完成这项工作,但是我正在寻找一种让Turtle模块本身完成此工作的方法.

I like what the turtle module does in Python and I'd like to output the entire animation of it drawing the shape. Is there a way to do this? GIF/MP4/anything that shows the animation. Note, I know that an external screen recorder will do the job, but I'm looking for a way for the turtle module to do this itself.

推荐答案

在OSX上使用预览"从Python乌龟制作动画GIF

1)从工作程序开始

很明显,在尝试生成动画GIF时不要调试代码.它应该是一个正确的乌龟程序,没有以mainloop()done()exitonclick()结尾的无限循环.

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)让您的程序定时保存快照

draw()save()stop()定时事件重新打包程序,大致如下:

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

我正在使用每秒10帧(FPS),因为这将与Preview在后续步骤中使用的帧匹配.

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

创建一个新的空目录,然后从那里运行它.如果一切按计划进行,它应该将一系列* .eps文件转储到目录中.

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.

假设预览是我的默认预览器,那么在Terminal.app中,我会简单地做:

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

open iceland*.eps

5)全选预览侧边栏中的PDF(均为EPS)文件和文件/导出... (不是导出为PDF )为GIF

选项按钮下设置导出类型,然后将其保存到我们的临时目录中.选择格式以查看GIF选项时,您需要按住 Option 键.选择一个好的屏幕分辨率.现在,我们应该在临时目录中具有* .gif文件. 退出预览.

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.

open iceland*.gif

7)将除第一个GIF文件外的所有文件合并到第一个GIF文件中

全选预览"边栏中的GIF文件.取消选择( Command 单击)第一个GIF文件,例如iceland001.gif.将选定的GIF文件拖到未选定的GIF文件上.这将修改它及其名称.使用 File/Export ... 将修改后的第一个GIF文件导出到新的GIF文件,例如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

通过将其加载到Safari中说服自己,例如:

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

open -a Safari iceland.gif

9)转换为重复的动画GIF

对于重复的动画GIF,您需要一些外部工具,例如 ImageMagick Gifsicle 来设置循环值:

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)动画GIF结果.祝你好运!

这篇关于有没有一种方法可以将乌龟的图形另存为GIF动画?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆