使用OSX上的预览从Python龟制作动画GIF
1)从工作程序开始
尽管看起来很明显,但在尝试生成动画GIF时不要调试代码.它应该是一个合适的海龟程序,没有无限循环,以mainloop(),done()或exitonclick()结束.
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()定时事件重新打包您的程序大致如下:
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),因为它与预览在后续步骤中使用的内容相匹配.
3)运行你的程序;完成后退出.
创建一个新的空目录并从那里运行它.如果一切都按计划进行,它应该将一系列* .eps文件转储到目录中.
4)将所有这些* .eps文件加载到预览中
假设Preview是我的默认预览器,在Terminal.app中,我只会这样做:
open iceland*.eps
5)选择 – 预览侧栏中的所有PDF(是EPS)文件和文件/导出…(不作为PDF导出)作为GIF
在“选项”按钮下设置导出类型,将它们保存到我们的临时目录中.在选择格式时,您需要按住Option键才能看到GIF选项.选择一个好的屏幕分辨率.我们现在应该在临时目录中有* .gif文件.
退出预览.
6)将所有* .gif文件加载到预览中
open iceland*.gif
7)将除第一个GIF文件之外的所有文件合并到第一个GIF文件中
在预览的侧边栏中选择所有GIF文件.取消选择(命令单击)第一个GIF文件,例如iceland001.gif.将选定的GIF文件拖到未选择的GIF文件上.这将修改它和它的名字.使用文件/导出…将修改后的第一个GIF文件导出到新的GIF文件,例如iceland.gif
8)这是一个动画GIF!
通过将其加载到Safari中来说服自己,例如:
open -a Safari iceland.gif
9)转换为重复的动画GIF
对于重复的动画GIF,您需要一些外部工具,如ImageMagick或Gifsicle来设置循环值:
convert -loop 0 iceland.gif iceland-repeating.gif
并再次使自己相信它有效:
open -a Safari iceland-repeating.gif
10)动画GIF结果.祝你好运!