"""
Created on Sun Sep 25 21:51:56 2022
@author: in
"""
import cv2
import os
from natsort import ns, natsorted
def Png_video(path,fps,ph_type):
ls=[]
files = os.listdir(path)
file_name = natsorted(files,alg=ns.PATH)
num = 0
for name in file_name:
splitext = os.path.splitext(name)
if splitext[1] == ph_type:
fname = os.path.join(path, name)
num += 1
print(num,"→",fname)
ls.append(fname)
print(f"共有{num}张照片待合并")
size = cv2.imread(ls[0]).shape[1::-1]
print(f"图片的像素→ 宽:{size[0]}像素、高:{size[1]}像素")
video_path = path+'\\'+"0result.mp4v"
videowriter = cv2.VideoWriter(video_path,-1, fps, size)
for x in ls:
img = cv2.imread(x)
videowriter.write(img)
videowriter.release()
print(f'完成制作,已保存至{video_path}')
if __name__ == '__main__':
Png_video(r"D:\20240225-1k\transient\pngvideo", 30,".png")
53 → D:\20240225-1k\transient\pngvideo\animation-2_0052.png
54 → D:\20240225-1k\transient\pngvideo\animation-2_0053.png
55 → D:\20240225-1k\transient\pngvideo\animation-2_0054.png
56 → D:\20240225-1k\transient\pngvideo\animation-2_0055.png
57 → D:\20240225-1k\transient\pngvideo\animation-2_0056.png
58 → D:\20240225-1k\transient\pngvideo\animation-2_0057.png
59 → D:\20240225-1k\transient\pngvideo\animation-2_0058.png
60 → D:\20240225-1k\transient\pngvideo\animation-2_0059.png
共有60张照片待合并
图片的像素→ 宽:815像素、高:558像素
完成制作,已保存至D:\20240225-1k\transient\pngvideo\0result.mp4v