You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

求可实现滑动图片拼接生成视频的Python脚本或库推荐

可用实现方案

你可以直接使用OpenCV搭配NumPy实现需求,无需引入复杂的重型库,自定义参数也非常灵活,以下是开箱可用的实现脚本:

依赖安装

执行以下命令安装所需依赖:

pip install opencv-python numpy

实现代码

import cv2
import numpy as np
import os

# 可自定义参数
image_folder = "你的图片文件夹路径"  # 替换为你的图片存储路径
output_video_path = "output_slide.mp4"
fps = 30  # 输出视频帧率
pixels_per_frame = 2  # 每帧滑动像素数,数值越大滑动速度越快
slide_direction = "left"  # 可选值:left/right/top/bottom
img_width, img_height = 1920, 1080  # 你的图片统一尺寸,需确保所有图片都是该尺寸

# 读取文件夹内所有图片
image_extensions = ('.png', '.jpg', '.jpeg', '.bmp')
images = []
for file_name in sorted(os.listdir(image_folder)):
    if file_name.lower().endswith(image_extensions):
        img = cv2.imread(os.path.join(image_folder, file_name))
        img = cv2.resize(img, (img_width, img_height))  # 强制统一尺寸,可注释如果已经保证尺寸一致
        images.append(img)
if not images:
    raise ValueError("文件夹内未找到符合要求的图片")

# 拼接长画布
if slide_direction in ["left", "right"]:
    # 水平拼接
    canvas = np.hstack(images)
    canvas_height, canvas_width = canvas.shape[:2]
    output_width, output_height = img_width, img_height
else:
    # 垂直拼接
    canvas = np.vstack(images)
    canvas_height, canvas_width = canvas.shape[:2]
    output_width, output_height = img_width, img_height

# 初始化视频写入器
fourcc = cv2.VideoWriter_fourcc(*'mp4v')
video_writer = cv2.VideoWriter(output_video_path, fourcc, fps, (output_width, output_height))

# 生成滑动帧
if slide_direction == "left":
    for x in range(0, canvas_width - output_width + 1, pixels_per_frame):
        frame = canvas[:, x:x+output_width]
        video_writer.write(frame)
elif slide_direction == "right":
    for x in range(canvas_width - output_width, -1, -pixels_per_frame):
        frame = canvas[:, x:x+output_width]
        video_writer.write(frame)
elif slide_direction == "top":
    for y in range(0, canvas_height - output_height + 1, pixels_per_frame):
        frame = canvas[y:y+output_height, :]
        video_writer.write(frame)
elif slide_direction == "bottom":
    for y in range(canvas_height - output_height, -1, -pixels_per_frame):
        frame = canvas[y:y+output_height, :]
        video_writer.write(frame)

# 释放资源
video_writer.release()
cv2.destroyAllWindows()

参数调整说明

  • 滑动方向:修改slide_direction参数即可切换左右上下四个滑动方向
  • 滑动速度:修改pixels_per_frame参数,数值越高每帧滑动距离越远,整体速度越快
  • 播放速度:修改fps参数,帧率越高视频播放越流畅,相同滑动参数下整体播放速度也越快
  • 如果需要给视频添加背景音乐,可以额外安装moviepy库,调用音频拼接接口即可实现

内容的提问来源于stack exchange,提问作者Dooderdo

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.23 14:15:00