Wrapping up answers, a short and simple "pythonesque" solution is to use that function:
import imageio
def make_mp4(moviename, fnames, fps):
with imageio.get_writer(moviename, mode='I', fps=fps) as writer:
for fname in fnames:
writer.append_data(imageio.imread(fname))
return 'done'
in conjunction with a loop my_loop
creating the images with a function create_image_as_numpy
such as:
fnames = []
np_dtype = np.uint8
for i_loop, _ in enumerate(my_loop):
imgnp = create_image_as_numpy(...)
fname = f'/tmp/frame_{i_loop}.png'
imageio.imwrite(fname, imgnp.astype(np_dtype))
fnames.append(fname)
make_mp4(video_name, fnames, fps=24)
I like to use the /tmp
folder (on Linux and MacOs) as it gets flushed at every reboot. Do do that flushing explictly, one can do:
import os
import imageio
def make_mp4(moviename, fnames, fps):
with imageio.get_writer(moviename, mode='I', fps=fps) as writer:
for fname in fnames:
writer.append_data(imageio.imread(fname))
for fname in fnames: os.remove(fname)
return 'done'
test.001.jpg
– Borrego