Python:使用 PIL 制作动画

kam*_*ame 2 python animation python-imaging-library

我想要一张动画图片。但我需要一个刷新功能,因为 plt.show() 总是打开一个新窗口。有人有提示吗?谢谢!

import numpy as np
import scipy
from scipy import *
import matplotlib.pyplot as plt

#array
aa = []
for x in range(44):
    aa.append([])
    for z in range(44):
        aa[x].append(3*sin(x/3.0)+2*cos(z/3.0))

b = aa
plt.imshow(b)
plt.show()

time = 0
dt = 0.1
while(time<3):
    b = sin(aa)
    time += dt
Run Code Online (Sandbox Code Playgroud)

Dan*_*aro 5

PIL 面向图像编辑,而不是动画或显示。相反,请查看 GUI 工具包或多媒体库,如pygletpygame


Gle*_*ies 5

除了有关 GUI 工具包的其他答案之外,您还可以保存图像并在事后构建动画。

如果图像颜色不够,PIL可以直接另存为gif,如这篇博文所示

相关部分:

frames = []
x, y = 0, 0
for i in range(10):
    new_frame = create_image_with_ball(400, 400, x, y, 40) # your imgs here
    frames.append(new_frame)
    x += 40 # you wont need these
    y += 40 # 

# Save into a GIF file that loops forever
frames[0].save('moving_ball.gif', format='GIF', append_images=frames[1:], save_all=True, duration=100, loop=0)
Run Code Online (Sandbox Code Playgroud)

需要根据您的目的进行修改,但我添加了一些注释来帮助您入门。