私信  •  关注

willem

willem 最近创建的主题
willem 最近回复了
4 年前
回复了 willem 创建的主题 » 在Python中将png文件转换为动画gif

import imageio
import pathlib
from datetime import datetime


def make_gif(image_directory: pathlib.Path, frames_per_second: float, **kwargs):
    """
    Makes a .gif which shows many images at a given frame rate.
    All images should be in order (don't know how this works) in the image directory

    Only tested with .png images but may work with others.

    :param image_directory:
    :type image_directory: pathlib.Path
    :param frames_per_second:
    :type frames_per_second: float
    :param kwargs: image_type='png' or other
    :return: nothing
    """
    assert isinstance(image_directory, pathlib.Path), "input must be a pathlib object"
    image_type = kwargs.get('type', 'png')

    timestampStr = datetime.now().strftime("%y%m%d_%H%M%S")
    gif_dir = image_directory.joinpath(timestampStr + "_GIF.gif")

    print('Started making GIF')
    print('Please wait... ')

    images = []
    for file_name in image_directory.glob('*.' + image_type):
        images.append(imageio.imread(image_directory.joinpath(file_name)))
    imageio.mimsave(gif_dir.as_posix(), images, fps=frames_per_second)

    print('Finished making GIF!')
    print('GIF can be found at: ' + gif_dir.as_posix())


def main():
    fps = 2
    png_dir = pathlib.Path('C:/temp/my_images')
    make_gif(png_dir, fps)

if __name__ == "__main__":
    main()

不幸的是,到目前为止唯一的解决办法是“等待并让它浸泡”,如 http://github.com/Microsoft/aspnet-docker/issues/120

谢谢@ikkentim指出这个问题。

我怀疑我的容器可能没有互联网接入,无法同步其时钟,但无法验证这一点,因为它今天早上才开始正常工作。

如果我找到比“等待”更有用的解决方案,我将发布一个更好的答案。