从 png 创建 gif:循环图像似乎太快并且只能在调试模式下工作

Creating a gif from pngs: looping through images seems too fast and only works in debug mode

我正在尝试从几个(在本例中只有 5 个)png 文件创建一个 gif 图像。如果我在图像循环发生时调试代码,我可以创建 gif。否则执行似乎太快了。这是我的代码:

img, *imgs = [Image.open(f) for f in sorted(glob.glob(png_in))]

所以我将列表理解展平并添加并尝试 time.sleep(x):

ImageFile.LOAD_TRUNCATED_IMAGES = True
imgs = []
for f in sorted(glob.glob(png_in)):
    i = Image.open(f)
    time.sleep(1)
    imgs.append(i)

然后我没有使用 PIL 模块,而是切换到 imageio 模块:

images = []
png_list = sorted(glob.glob(png_in))
for filename in png_list:
    print(f"fn: {filename}")
    images.append(imageio.imread(filename))
    time.sleep(1)
imageio.mimsave(gif_file, images)

我有什么地方可能出错的想法吗?例如,我收到的错误消息是:

unknown element "blank"
    i = Image.open(f)

感谢 Mark Setchell....我发现的问题是在执行 for 循环时 png 并不全部存在。我正在使用 openscad 创建 png 文件,在这里我需要实现一个 time.sleep。在调试模式下,因为速度较慢,openscad 有时间创建 png,所以您不会看到这个问题。