wha*_*hat 3 python gif turtle-graphics
我喜欢乌龟模块在 Python 中所做的,我想输出它绘制形状的整个动画。有没有办法做到这一点?GIF/MP4/任何显示动画的东西。请注意,我知道外部屏幕录像机可以完成这项工作,但我正在寻找一种让乌龟模块自己完成这项工作的方法。
cdl*_*ane 10
看起来很明显,在尝试生成动画 GIF 时不要调试代码。它应该是一个合适的海龟程序,没有以mainloop()、done()、 或结尾的无限循环exitonclick()。
我将用于此解释的程序是我为编程拼图和高尔夫代码编写的程序,该程序使用乌龟绘制冰岛国旗。它是有意的极简主义,因为它是 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()
Run Code Online (Sandbox Code Playgroud)
使用draw(),save()和stop()定时事件重新打包您的程序,大致如下:
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()
Run Code Online (Sandbox Code Playgroud)
我使用的是每秒 10 帧 (FPS),因为这将与 Preview 在后面的步骤中使用的相匹配。
创建一个新的空目录并从那里运行它。如果一切按计划进行,它应该将一系列 *.eps 文件转储到目录中。
假设 Preview 是我的默认预览器,在 Terminal.app 中我会简单地做:
open iceland*.eps
Run Code Online (Sandbox Code Playgroud)
在选项按钮下设置导出类型,将它们保存到我们的临时目录中。选择格式时需要按住Option键才能查看 GIF 选择。选择一个好的屏幕分辨率。现在我们的临时目录中应该有 *.gif 文件。 退出预览。
open iceland*.gif
Run Code Online (Sandbox Code Playgroud)
在预览的边栏中选择所有GIF 文件。取消选择(命令单击)第一个 GIF 文件,例如iceland001.gif. 将选中的 GIF 文件拖到未选中的 GIF 文件上。这将修改它及其名称。使用File/Export...将修改后的第一个 GIF 文件导出到一个新的 GIF 文件,例如iceland.gif
通过将其加载到 Safari 中来说服自己,例如:
open -a Safari iceland.gif
Run Code Online (Sandbox Code Playgroud)
对于重复的动画 GIF,您需要一些外部工具(如ImageMagick或Gifsicle)来设置循环值:
convert -loop 0 iceland.gif iceland-repeating.gif
Run Code Online (Sandbox Code Playgroud)
并再次说服自己它有效:
open -a Safari iceland-repeating.gif
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
6176 次 |
| 最近记录: |