0

我编写了一个 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 的 Image 模块来制作我的 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')
4

1 回答 1

0

您可以将参数、循环和持续时间都传递给 mimsave/mimwrite 方法。

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

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

imageio.help("gif")

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

A format for reading and writing static and animated GIF, based
on Pillow.

Images read with this format are always RGBA. Currently,
the alpha channel is ignored when saving RGB images with this
format.

Parameters for reading
----------------------
None

Parameters for saving
---------------------
loop : int
    The number of iterations. Default 0 (meaning loop indefinitely).
duration : {float, list}
    The duration (in seconds) of each frame. Either specify one value
    that is used for all frames, or one value for each frame.
    Note that in the GIF format the duration/delay is expressed in
    hundredths of a second, which limits the precision of the duration.
fps : float
    The number of frames per second. If duration is not given, the
    duration for each frame is set to 1/fps. Default 10.
palettesize : int
    The number of colors to quantize the image to. Is rounded to
    the nearest power of two. Default 256.
subrectangles : bool
    If True, will try and optimize the GIF by storing only the
    rectangular parts of each frame that change with respect to the
    previous. Default False.
于 2019-03-06T02:50:53.670 回答