imageio 图片转mp4 保存mp4

目录

安装:

[imageio 图片转mp4](#imageio 图片转mp4)

[numpy 保存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)
相关推荐
kkoral11 分钟前
如何在 Python 中使用 OpenCV 调用 FFmpeg 的特定功能?
python·opencv·ffmpeg
樹JUMP13 分钟前
Python虚拟环境(venv)完全指南:隔离项目依赖
jvm·数据库·python
free_7315 分钟前
超越“回答”,AI Agent迎来全链路安全治理挑战
人工智能·python·网络安全
sg_knight16 分钟前
设计模式实战:策略模式(Strategy)
java·开发语言·python·设计模式·重构·架构·策略模式
飞Link24 分钟前
告别 ROS 的臃肿:用 ZeroMQ 构建极速具身智能分布式大脑(附 Python 实战)
开发语言·分布式·python
嫂子的姐夫27 分钟前
039-DES:gov招标(解密)
爬虫·python·js逆向·逆向
jgyzl30 分钟前
2026.3.20 用EasyExcel实现excel报表的导入与导出
java·python·excel
weixin_4331793338 分钟前
python - 正则表达式Regex
python·正则表达式
Riemann~~44 分钟前
ros2写一个可以修改参数的node
开发语言·python·ros2·机器人系统
郝学胜-神的一滴1 小时前
PyTorch 张量基础:零张量/一张量/指定值张量全解析
人工智能·pytorch·python