简介:python将一张图片转视频
python提供了两个库实现将一张图片转视频,分别是moviepy和opencv
moviepy将一张图片生成视频
def movie_video(image_path,output_path):
'''将一张图片合成视频
image_path:图片路径
output_path:输出路径
'''
from moviepy.editor import ImageClip,ImageSequenceClip
# 创建一个ImageClip对象,设置持续时间
clip = ImageClip(image_path, duration=10)
# 写入视频文件
clip.write_videofile(output_path, codec='libx264',fps=30)
opencv将一张图片合成视频
def img_video(image_path,output_path):
'''将一张图片合成视频
image_path:图片路径
output_path:输出路径
'''
import cv2,numpy as np
# 读取图片
frame = cv2.imread(image_path)
height, width, layers = frame.shape
# 设置视频的编码器、帧率、尺寸
size = (width, height)
out = cv2.VideoWriter(output_path, cv2.VideoWriter_fourcc(*'mp4v'), 30, size)
# 假设你想要一个10秒的视频
duration = 10 # in seconds
frames_to_write = int(duration * 30) # 帧率 * 时长
# 将同一张图片写入多次来创建视频
for _ in range(frames_to_write):
out.write(frame)
# 释放VideoWriter对象
out.release()
有遗漏或者不对的可以在我的公众号留言哦