Python中读取指定文件夹多个图片使用视频播放及视频图片格式转换

1、视频转图片

#!/usr/bin/env python  
import cv2  
vc=cv2.VideoCapture("Your_Vid's_Name.mp4")  
c=1  
if vc.isOpened():  
    rval,frame=vc.read()  
else:  
    rval=False  
while rval:  
    rval,frame=vc.read()  
    cv2.imwrite('The_Path_to_Save_Your_Pics'+str(c)+'.jpg',frame)  
    c=c+1  
    cv2.waitKey(1)  
vc.release() 

2、图片转视频文件

#!/usr/bin/env python  
import cv2  
from cv2 import VideoWriter,VideoWriter_fourcc,imread,resize  
import os  
img_root="The_Address_of_Your_Pics'_folder"  
#Edit each frame's appearing time!  
fps=5  
fourcc=VideoWriter_fourcc(*"MJPG")  
videoWriter=cv2.VideoWriter("The_Way_You_Want_to_Save_Your_Vid.avi",fourcc,fps,(1200,1200))  
  
im_names=os.listdir(img_root)  
for im_name in range(len(im_names)):  
    frame=cv2.imread(img_root+str(im_name)+'.jpg')  
    print im_name  
    videoWriter.write(frame)  
      
videoWriter.release()  

3、Python中读取指定文件夹多个图片使用视频播放

def readImageShowVideo(imagepath):
    img_path = gb.glob(os.path.join(imagepath, '*.jpg'))
    print(img_path)
    # img_path = "PETS2006/ROI.bmp"
    for path in img_path:
        img = cv2.imread(path)
        cv2.imshow('frame', img)
        cv2.waitKey(30)

分享无价,转载请注明出处!

https://my.oschina.net/u/3702502/blog/1819618

猜你喜欢

转载自my.oschina.net/u/3702502/blog/1819618