# -*- coding: utf-8 -*- """ Created on Tue Jan 30 19:15:05 2024 @author: ym """ import cv2 import os import glob IMG_FORMATS = "bmp", "dng", "jpeg", "jpg", "mpo", "png", "tif", "tiff", "webp", "pfm" # include image suffixes VID_FORMATS = "asf", "avi", "gif", "m4v", "mkv", "mov", "mp4", "mpeg", "mpg", "ts", "wmv" # include video suffixes def for_test(): save_path = video_path + img_path fps, w, h = 10, 1024, 1280 cap = cv2.VideoWriter(save_path, cv2.VideoWriter_fourcc(*'mp4v'), fps, (w, h)) pathx = path + img_path imgfiles = [f for f in os.listdir(pathx) if not f.find("_cut") != -1] imgfiles.sort(key = lambda x: int(x[:-5])) imgpaths = [] for imgfile in imgfiles: imgpaths.append(os.path.join(pathx, imgfile)) center = (1280/2, 1024/2) rotate_matrix = cv2.getRotationMatrix2D(center=center, angle=-90, scale=1) k = 0 for ipath in imgpaths: img = cv2.imread(ipath) rotated_image = cv2.warpAffine(src=img, M=rotate_matrix, dsize=(w, h)) cap.write(rotated_image) print("Have imgs") def test_1(): # name = os.path.split(img_path)[-1] # save_path = video_path + name + '.mp4' save_path = video_path + img_path fps, w, h = 10, 1024, 1280 cap = cv2.VideoWriter(save_path, cv2.VideoWriter_fourcc(*'mp4v'), fps, (w, h)) pathx = path + img_path imgfiles = [f for f in os.listdir(pathx) if not f.find("_cut") != -1] imgfiles.sort(key = lambda x: int(x[:-5])) imgpaths = [] for imgfile in imgfiles: imgpaths.append(os.path.join(pathx, imgfile)) # ipaths = [os.path.join(pathx, f) for f in os.listdir(pathx) if not f.find("_cut") != -1] # ipaths = [] # for f in os.listdir(pathx): # if not f.find('_cut'): # ipaths.append(os.path.join(pathx, f)) # ipaths.sort(key = lambda x: int(x.split('_')[-2])) k = 0 for ipath in imgpaths: img = cv2.imread(ipath) cap.write(img) k += 1 cap.release() print(img_path + f" have imgs: {k}") def img2video(imgpath): if not os.path.isdir(imgpath): return files = [] files.extend(sorted(glob.glob(os.path.join(imgpath, "*.*")))) images = [x for x in files if x.split(".")[-1].lower() in IMG_FORMATS] h, w = cv2.imread(images[0]).shape[:2] fps = 25 vidpath = imgpath + '.mp4' cap = cv2.VideoWriter(vidpath, cv2.VideoWriter_fourcc(*'mp4v'), fps, (w, h)) for p in images: img = cv2.imread(p) cap.write(img) cap.release() def main(): imgpath = r"D:\work\result\202503251112_v10s_result" img2video(imgpath) if __name__ == "__main__": main()