Creating Video from JPG or PNG images using OpenCV-python

转自
Steps:

  1. Fetch all the image file names using glob
  2. Read all the images using cv2.imread()
  3. Store all the images into a list
  4. Create a VideoWriter object using cv2.VideoWriter()
  5. Save the images to video file using cv2.VideoWriter().write()
  6. Release the VideoWriter and destroy all windows.
import cv2
import numpy as np
import glob

img_array = []
# glob.glob(正则表达式), 返回一个列表
for filename in glob.glob('C:/New folder/Images/*.jpg'):
    img = cv2.imread(filename)
    height, width, layers = img.shape
    size = (width,height)
    img_array.append(img)


out = cv2.VideoWriter('project.avi',cv2.VideoWriter_fourcc(*'DIVX'), 15, size)
 
for i in range(len(img_array)):
    out.write(img_array[i])
out.release()

or
不使用列表,每次读取完后释放内存

import cv2
import numpy as np
import glob

img_array = []
size = (1226, 370)
out = cv2.VideoWriter('project1.avi', cv2.VideoWriter_fourcc(*'DIVX'), 10, size)
for filename in glob.glob(r'E:\8_DataSet\KITTI\KITTITrackingdata_tracking_image_2\testing\image_02\0028/*.png'):
    img = cv2.imread(filename)
    height, width, layers = img.shape

    size = (width, height)
    img_array.append(img)

    out.write(img)

jpg转MP4

import cv2
import numpy as np
import glob

img_array = []
# glob.glob(正则表达式), 返回一个列表
for filename in glob.glob(r'C:\Users\lpf\Desktop\MOT16\test\MOT16-01\img1/*.jpg'):
    img = cv2.imread(filename)
    height, width, layers = img.shape
    size = (width, height)
    img_array.append(img)

out = cv2.VideoWriter('project.mp4', cv2.VideoWriter_fourcc(*'mp4v'), 30, size)

for i in range(len(img_array)):
    out.write(img_array[i])
out.release()
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值