python制作gif动图,在python中使用imageio从图像制作gif

在尝试使用imageio库从一系列PNG图片创建GIF动画时,遇到问题,只显示第一帧并快速结束。尝试了两种不同的方法,但都无法正常工作。最终找到一个有效的方法:通过遍历指定目录下的所有PNG文件,使用imageio的mimsave函数成功创建了动画。
摘要由CSDN通过智能技术生成

I have tried reading a lot of examples online and found imageio is the perfect package for it. Also found examples written in here.

I have just followed the example as shown and tried the following

import imageio as io

import os

file_names = sorted((fn for fn in os.listdir('.') if fn.startswith('surface')))

#making animation

with io.get_writer('surface.gif', mode='I', duration=0.5) as writer:

for filename in file_names:

image = io.imread(filename)

writer.append_data(image)

writer.close()

and another example.

images = []

for filename in file_names:

images.append(io.imread(filename))

io.mimsave('surface1.gif', images, duration = 0.5)

both of these do not work. And basically i only see the first frame from the gif and a blink and finish. The duration is set 0.5secs, so it should work fine. I might have been missing out something here.

解决方案

This works for me:

import os

import imageio

png_dir = '../saves/png/'

images = []

for file_name in os.listdir(png_dir):

if file_name.endswith('.png'):

file_path = os.path.join(png_dir, file_name)

images.append(imageio.imread(file_path))

imageio.mimsave('../saves/gif/movie.gif', images)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值