I need to create a GIF from the last seven newest images from a directory. The directory is constantly updating and adding new images every 10 minutes, so I already could make the code to create the GIF including all the files in the directory, but I need to be more specific and just create a GIF from the last seven newest images. In conclusion: I need to specify the limit of images which my code has to grab to create the GIF.. Below is the code so far:
import imageio
import os
import time
now = time.time()
png_dir = 'C:/Users/dchacon/Desktop/Destino/'
images = []
for file_name in os.listdir(png_dir):
if file_name.endswith('.png'):
file_path = os.path.join(png_dir, file_name)
images.append(imageio.imread(file_path))
imageio.mimsave('C:/Users/dchacon/Desktop/Destino/NTMPCA.gif', images, fps=1)
I had the same idea as suggested in Mark's comment. For getting an appropriate list of all files in the directory, sorted by modification data, please see this helpful Q&A. We just need to add
reverse=True
in thesorted
call of the accepted answer, such that we have the newest images at the list's start. The rest is some list comprehension and your saving call:I get a proper GIF of the six newest images in my test directory.