更改gif特征

时间:2019-03-05 17:06:55

标签: python-3.x python-imaging-library python-imageio

我编写了一个python代码,该代码从图像列表中创建了gif。为了做到这一点,我使用了python库:imageio。这是我的代码:

def create_gif(files, gif_path):
"""Creates an animated gif from a list of figures

Args:
    files (list of str) : list of the files that are to be used for the gif creation.
        All files should have the same extension which should be either png or jpg
    gif_path (str) : path where the created gif is to be saved

Raise:
    ValueError: if the files given in argument don't have the proper 
        file extenion (".png" or ".jpeg" for the images in 'files',
        and ".gif" for 'gif_path')
"""
images = []

for image in files:

    # Make sure that the file is a ".png" or a ".jpeg" one
    if splitext(image)[-1] == ".png" or splitext(image)[-1] == ".jpeg":
        pass
    elif splitext(image)[-1] == "":
        image += ".png"
    else:
        raise ValueError("Wrong file extension ({})".format(image))
    # Reads the image with imageio and puts it into the images list
    images.append(imageio.imread(image))

# Mak sure that the file is a ".gif" one
if splitext(gif_path)[-1] == ".gif":    
    pass
elif splitext(gif_path)[-1] == "":
    gif_path += ".gif"
else:
    raise ValueError("Wrong file extension ({})".format(gif_path))

# imageio writes all the images in a .gif file at the gif_path   
imageio.mimsave(gif_path, images)

当我尝试使用带有图像列表的代码时,会正确创建Gif,但是我不知道如何更改其参数: 我的意思是,我希望能够控制gif图像之间的延迟,并控制gif的运行时间。

我尝试使用PIL中的“图像”模块对gif进行更改,并更改其信息,但是当我保存它时,gif变成了我的第一张图像。

您能帮我了解我在做什么错吗?

这是我为尝试更改gif参数而运行的代码:

# Try to change gif parameters
my_gif = Image.open(my_gif.name)
my_gif_info = my_gif.info
print(my_gif_info)
my_gif_info['loop'] = 65535
my_gif_info['duration'] = 100
print(my_gif.info)
my_gif.save('./generated_gif/my_third_gif.gif')

1 个答案:

答案 0 :(得分:0)

您只需将循环和持续时间这两个参数传递给mimsave / mimwrite方法。

$

下次您要检查哪些参数可用于与imageio兼容的格式时,只需使用imageio.help(格式名称)。

imageio.mimsave(gif_name, fileList, loop=4, duration = 0.3)

GIF-PIL-静态和动态gif(枕头)

imageio.help("gif")