背景
由于作业需要,尝试了图片与视频的相互转化,奈何看到很多文章尝试后总会出点问题,因此这里一并记录解决 不过目前图片转视频后仍然存在闪烁问题,但看到较好的解决方法后再更新
视频转图片
详见注释
import cv2
def getpic(videoPath, svPath):
cap = cv2.VideoCapture(videoPath)
numFrame = 0
while True:
if cap.grab():
flag, frame = cap.retrieve()
if not flag:
continue
else:
numFrame += 1
newPath = svPath + str(numFrame) + ".jpg"
cv2.imencode('.jpg', frame)[1].tofile(newPath)
print(numFrame)
else:
break
getpic("test.mp4", "G:/zanshipic/")
print("all is ok")
主要参考自:https://blog.csdn.net/u010555688/article/details/79182362
图片转视频
详见注释
import cv2
import os
img_array = []
path = "G:/zanshipic/"
filelist = os.listdir(path)
for filename in filelist:
img = cv2.imread(path+filename)
height, width, layers = img.shape
size = (width, height)
img_array.append(img)
print("this is ok")
fps=30
videopath='G:/test10.avi'
out1 = cv2.VideoWriter(videopath,cv2.VideoWriter_fourcc(*'DIVX'),fps, size)
for i in range(len(img_array)):
out1.write(img_array[i])
out1.release()
print("all is ok")
主要参考自:https://blog.csdn.net/kxh123456/article/details/121692474
|