As an additional contribution, if anyone struggles to get the loaded images to be sorted alphabetically, here’s a version that uses the built-in sorted(os.listdir(image_folder)
) function.
import os
import moviepy.video.io.ImageSequenceClip
from PIL import Image, ImageFile
ImageFile.LOAD_TRUNCATED_IMAGES = True
image_folder='frames'
fps=10
image_files = [os.path.join(image_folder,img)
for img in sorted(os.listdir(image_folder))
if img.endswith(".png")]
print(image_files)
clip = moviepy.video.io.ImageSequenceClip.ImageSequenceClip(image_files, fps=fps)
clip.write_videofile('myvideo.mp4')
However, note that it is filesystem/filename dependant and will sort numbers wrong (1,10,2,3…). Below is another solution with a sorted_alphanumeric(data)
function that solves that (from another thread)
import os
import moviepy.video.io.ImageSequenceClip
from PIL import Image, ImageFile
ImageFile.LOAD_TRUNCATED_IMAGES = True
image_folder='frames'
fps=10
import re
def sorted_alphanumeric(data):
convert = lambda text: int(text) if text.isdigit() else text.lower()
alphanum_key = lambda key: [ convert(c) for c in re.split('([0-9]+)', key) ]
return sorted(data, key=alphanum_key)
image_files = [os.path.join(image_folder,img)
for img in sorted_alphanumeric(os.listdir(image_folder))
if img.endswith(".png")]
print(image_files)
clip = moviepy.video.io.ImageSequenceClip.ImageSequenceClip(image_files, fps=fps)
clip.write_videofile('myvideo.mp4')