Python:使用PIL制作动画

6

我想要一个动态图片。但是我需要一个刷新功能,因为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

5
为什么不使用 PyOpenGL、Pygame 或其他更适合此任务的库呢?为什么要选择 PIL? - batbrat
2个回答

8

PIL主要用于图像编辑,而不是动画或显示。因此,可以考虑使用GUI工具包或多媒体库,例如pygletpygame


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)

需要进行修改以适应您的目的,但我已添加了一些注释帮助您入门。

不错,代码相当简单,非常适合快速制作 GIF。 - Dan

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接