我需要用 python 中的图像序列创建一个视频。我在网上发现这段代码工作正常,但在用 python 读取图像时,我遇到了一个小问题。即使文件夹中的顺序没问题。例如,frame100.jpg,frame101.jpg,frame102.jpg,.....,frame1000,frame1001,...当我在循环内用python读取它们时,调试后我看到以下'frame100.jpg','帧1000.jpg','帧1001.jpg','帧1002.jpg',......,帧101,帧1010,帧1011......
这是代码
def images_to_video():
image_folder = 'data_out'
images = [img for img in os.listdir(image_folder) if img.endswith(".jpg")]
frame = cv2.imread(os.path.join(image_folder, images[0]))
height, width, layers = frame.shape
video = cv2.VideoWriter('project.avi',cv2.VideoWriter_fourcc(*'DIVX'), 15, (width,height))
for image in images:
video.write(cv2.imread(os.path.join(image_folder, image)))
cv2.destroyAllWindows()
video.release()
Run Code Online (Sandbox Code Playgroud)