python 画动图
时间: 2023-11-07 13:01:52 浏览: 44
你可以使用 Python 中的 imageio 库来生成动态图像(gif)。首先,你需要将所有要生成动态图像的图像文件路径存储到一个列表中。然后,使用 imageio.mimsave 函数将这些图像文件转换为动态图像。你可以指定动态图像的保存路径和每个图像之间的延迟时间。
下面是一个示例代码:
```python
import os
import imageio
def gif_generator():
path = './imgs' # 图像文件夹路径
filenames = []
# 遍历文件夹,将图像文件的路径存储到列表中
for file in os.listdir(path):
if file.endswith('jpg') or file.endswith('jpeg') or file.endswith('png'):
filename = os.path.join(path, file)
filenames.append(filename)
images = []
# 读取每个图像文件,并添加到图像列表中
for filename in filenames:
images.append(imageio.imread(filename))
# 保存动态图像,指定保存路径和每个图像之间的延迟时间
imageio.mimsave('./OR_perceptron.gif', images, duration=0.8)
```
你可以将上述代码保存为一个名为 gif_generator 的 Python 文件,并在你的绘图主程序中导入并调用该函数即可生成 gif 图像。