目录
安装:
imageio 图片转mp4
numpy 保存mp4
安装:
FFMPEG: pip install imageio[ffmpeg]
pyav: pip install imageio[pyav]
imageio 图片转mp4
python">import glob
import os
import cv2
import imageio
from natsort import natsorted
from PIL import Image
import numpy as np
import orjson
def pic_2_mp4_dir(path):
dir_root = r'E:\project\Grounded-SAM-2-main\obj_video\0'
dir_save = r'E:\project\Grounded-SAM-2-main\obj_video'
# dirs = glob.glob(dir_root + '/*/*')
dirs=[r'E:\project\Grounded-SAM-2-main\obj_video\0']
for dir_path in dirs:
img_files = ['%s/%s' % (i[0].replace("\\", "/"), j) for i in os.walk(dir_path) for j in i[-1] if j.endswith(('obj.jpg', 'apng', 'ajpeg'))]
print("len(img_files)", len(img_files), dir_path)
img_files = natsorted(img_files)
imgs = []
for img_i, img_path in enumerate(img_files):
print(img_path)
output_image = Image.open(img_path)
imgs.append(output_image)
save_dir = dir_path.replace(dir_root, dir_save)
os.makedirs(save_dir, exist_ok=True)
imageio.mimsave(f'{save_dir}/pinjie.mp4', imgs, fps=6)
def pic_2_mp4(base_dir,save_path):
img_files = ['%s/%s' % (i[0].replace("\\", "/"), j) for i in os.walk(base_dir) for j in i[-1] if j.endswith(('.jpg', 'apng', 'ajpeg'))]
print("len(img_files)", len(img_files), base_dir)
img_files = natsorted(img_files)
imgs = []
for img_i, img_path in enumerate(img_files):
if img_i%10==0:
print(img_i,img_path)
output_image = Image.open(img_path)
imgs.append(output_image)
os.makedirs(os.path.dirname(save_path), exist_ok=True)
imageio.mimsave(save_path, imgs, fps=25)
if __name__ == '__main__':
base_dir = r'F:\project\ronghe\data\see3d_inpaint\vis_see3d_inpaint\f2_1200_dir\pinjie'
save_path = r'F:\project\ronghe\data\see3d_inpaint\vis_see3d_inpaint\f2_1200_dir\pinjie.mp4'
pic_2_mp4(base_dir, save_path)
numpy 保存mp4
python"> save_video_path = os.path.join(out_dir, output_video_name)
print("save_video_path = ", save_video_path, "; ", video_codec, ", ", fps, ", ", size, ", video_size = ", video_size)
imgs=[]
for i in tqdm(range(num_frames)):
# Process image
deg = i * interval_deg
img = equ.GetPerspective(fov, deg, 0, *video_size) # Specify parameters(FOV, theta, phi, height, width)
if margin > 0:
img = img[margin:-margin]
img = np.clip(img, 0, 255).astype(np.uint8)
img=cv2.cvtColor(img,cv2.COLOR_BGR2RGB)
imgs.append(Image.fromarray(img))
imageio.mimsave(save_video_path, imgs, fps=fps,macro_block_size=None)