用Python将图像以任意顺序制作成GIF

4

我正在尝试使用Python语言在Ubuntu 12.04中将一系列PNG格式的图片制作成GIF。我有一个包含这些图片的文件,它们的名称为lip_shapes1.png到lip_shapes11.png。此外,我还有一个包含图像名称的列表,我想按照这个顺序制作GIF。列表如下:

list = [lip_shapes1.png, lip_shapes4.png , lip_shapes11.png, lip_shapes3.png]

但我的问题是我找到了这段代码:
import os
os.system('convert   -loop 0   lip_shapes*.gnp   anime.gif')

但它只按照GNP名称的顺序制作gif,但我想让它按照我想要的任何顺序制作。这是可能的吗?

如果有人能帮助我,我真的很感激。

提前致谢

PS:我还想把它拍成电影。我尝试了这个代码(shapes是我的图片名称列表):

    s = Popen(['ffmpeg', '-f', 'image2', '-r', '24', '-i'] + shapes + ['-vcodec', 'mpeg4', '-y', 'movie.mp4'])
s.communicate()

但是在终端中,它给了我这个错误信息并且无法工作:

FFmpeg程序仅用于脚本兼容性,并将在未来的版本中被删除。它已经在Libav项目中被弃用,以允许在其替代品avconv中实现不兼容的命令行语法改进(有关详细信息,请参见Changelog)。请改用avconv。

从'shz8.jpeg'输入#0,image2: Duration: 00:00:00.04,start: 0.000000,bitrate:N/A Stream #0.0:Video:mjpeg,yuvj420p,266x212 [PAR 1:1 DAR 133:106],24 tbr,24 tbn,24 tbc

'shz8.jpeg'是列表中的第一个名称。

谢谢


3
lip_shapes1.pnglip_shapes2.pnglip_shapes3.pnglip_shapes4.png等图片循环播放,并将它们转换为anime.gif动画文件,命令为:convert -loop 0 lip_shapes1.png lip_shapes2.png lip_shapes3.png lip_shapes4.png ... anime.gif - Mark Setchell
3个回答

2
如果您使用subprocess.call,可以将文件名作为字符串列表传递。这样可以避免出现shell引号问题,例如,如果文件名包含引号或空格。
import subprocess
shapes = ['lip_shapes1.png', 'lip_shapes4.png' , 'lip_shapes11.png', 'lip_shapes3.png']
cmd = ['convert', '-loop0'] + shapes + ['anime.gif']
retcode = subprocess.call(cmd)
if not retval == 0:
    raise ValueError('Error {} executing command: {}'.format(retcode, cmd))    

1

所以你有一个图像列表,你想将其作为Python列表转换为gif。您可以按任何顺序对其进行排序或排列。例如

img_list = ['lip_shapes1.png', 'lip_shapes4.png', 'lip_shapes11.png', 'lip_shapes3.png'] img_list.sort()

请注意,不应使用list作为变量名,因为它是列表类型的名称。

然后,您可以在调用os.system(convert ...)时使用此列表,例如

os.system('convert -loop 0 %s anime.gif'%' '.join(img_list))


我使用了你的方法,那正是我想要的。 - Shahrzad A
@ShahrzadA:如果你认为有用的话,那么请接受我的帖子(在下面打勾)或者点赞。 - running.t
你也知道如何从之前提到的一系列图像中制作MP4吗? - Shahrzad A
@ShahrzadA:你应该为此提出单独的问题。然而,我认为这个问题超出了stackoverflow的范围,所以更好的提问地点是SuperUserAskUbuntu - running.t

0
如果你想从文件夹中读取一系列的png文件,建议使用for循环来检查文件的结尾,例如.png、.jpg等。在这里,你需要确保处理好几件事情。我写了一篇关于如何轻松实现这个功能的博客文章(点击这里阅读)。
image_file_names = [],[]
for file_name in os.listdir(png_dir):
    if file_name.endswith('.png'):
        image_file_names.append(file_name)
        sorted_files = sorted(image_file_names, key=lambda y: int(y.split('_')[1]))

这将把所有的'.png'文件放入一个文件名向量中。从那里,您可以通过循环遍历文件来使用以下内容自定义gif:

images = []
frame_length = 0.5 # seconds between frames
end_pause = 4 # seconds to stay on last frame
# loop through files, join them to image array, and write to GIF called 'test.gif'
for ii in range(0,len(sorted_files)):       
    file_path = os.path.join(png_dir, sorted_files[ii])
    if ii==len(sorted_files)-1:
        for jj in range(0,int(end_pause/frame_length)):
            images.append(imageio.imread(file_path))
    else:
        images.append(imageio.imread(file_path))
# the duration is the time spent on each image (1/duration is frame rate)
imageio.mimsave('test.gif', images,'GIF',duration=frame_length)

以下是使用上述方法生成的示例:

GIF Example

https://engineersportal.com/blog/2018/7/27/how-to-make-a-gif-using-python-an-application-with-the-united-states-wind-turbine-database


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