图片生成视频
opencv-python · PyPI https://pypi.org/project/opencv-python/
nstallation and Usage
- If you have previous/other version of OpenCV installed (e.g. cv2 module in the root of Python’s site-packages), remove it before installation to avoid conflicts.
- To further avoid conflicts and to make development easier, Python’s virtual environments are highly recommended for development purposes.
- If you have an existing opencv-contrib-python installation, run pip uninstall opencv-contrib-python
- Install this package:
pip install opencv-python
- Import the package:
import cv2
注意 路径中英文
'''
python+opencv视频图像相互转换 - CSDN博客 https://blog.csdn.net/m0_37733057/article/details/79023693
链接:https://www.zhihu.com/question/49558804/answer/343058915
'''
import cv2
import glob
import os, sys
import ffmpeg
os_sep = os.sep
this_file_abspath = os.path.abspath(__file__)
this_file_dirname, this_file_name = os.path.dirname(this_file_abspath), os.path.abspath(__file__).split(os_sep)[
-1]
base_dir = os.path.dirname(this_file_dirname)
fps = 120 # 保存视频的FPS,可以适当调整
fourcc = cv2.VideoWriter_fourcc('M', 'J', 'P', 'G') # opencv3.0
import time
f_v = '{}{}'.format(int(time.time()), 'saveVideo.avi')
imgs = glob.glob('C:\\Users\\sas\\PycharmProjects\\mypygame\\mypng\\*.jpg')
for imgname in imgs:
print(imgname)
img = cv2.imread(imgname)
img_size = (img.shape[1], img.shape[0])
videoWriter = cv2.VideoWriter(f_v, fourcc, fps, img_size)
frame = cv2.imread(imgname)
videoWriter.write(frame)
videoWriter.release()
'''
python+opencv视频图像相互转换 - CSDN博客 https://blog.csdn.net/m0_37733057/article/details/79023693
链接:https://www.zhihu.com/question/49558804/answer/343058915
'''
import cv2
import glob
import os, sys
import ffmpeg
os_sep = os.sep
this_file_abspath = os.path.abspath(__file__)
this_file_dirname, this_file_name = os.path.dirname(this_file_abspath), os.path.abspath(__file__).split(os_sep)[
-1]
base_dir = os.path.dirname(this_file_dirname)
fps = 100 # 保存视频的FPS,可以适当调整
fourcc = cv2.VideoWriter_fourcc('M', 'J', 'P', 'G') # opencv3.0
import time
f_v = '{}{}'.format(int(time.time()), 'saveVideo.avi')
imgs = glob.glob('C:\\Users\\sas\\PycharmProjects\\mypygame\\mypng\\*.jpg')
img = cv2.imread(imgs[0])
img_size = (img.shape[1], img.shape[0])
videoWriter = cv2.VideoWriter(f_v, fourcc, fps, img_size)
for imgname in imgs:
myinterval = 2
this_time = time.time()
while time.time() - this_time < 2:
print(imgname)
img = cv2.imread(imgname)
frame = cv2.imread(imgname)
videoWriter.write(frame)
videoWriter.release()

浙公网安备 33010602011771号