Python Code:图片和视频互相转换

问题:

把一个视频,按照每一帧输出成一系列的图片,放在对应文件夹;

把特定文件夹下的一系列图片做成一个视频,并且每帧出现频率可以调整;

代码一、视频转图片:

#!/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()
代码二、图片转视频:
#!/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()


猜你喜欢

转载自blog.csdn.net/Errors_In_Life/article/details/72809580