[441]python3图视频互转

  • 图转视频
# -*- coding:utf-8 -*-
import cv2,glob


fps = 20   #保存视频的FPS,可以适当调整

#可以用(*'DVIX')或(*'X264'),如果都不行先装ffmepg: pip install ffmepg
fourcc = cv2.VideoWriter_fourcc(*'MJPG')
#saveVideo.avi是要生成的视频名称,(384,288)是图片尺寸
videoWriter = cv2.VideoWriter('saveVideo.avi',fourcc,fps,(384,288))
#imge存放图片
imgs=glob.glob('imge/*.jpg')
for imgname in imgs:
    frame = cv2.imread(imgname)
    videoWriter.write(frame)
videoWriter.release()
  • 视频转图
# -*- coding:utf-8 -*-
import cv2


vc = cv2.VideoCapture('a.avi') #读入视频文件
c=0
rval=vc.isOpened()
#timeF = 1  #视频帧计数间隔频率
while rval:   #循环读取视频帧
    c = c + 1
    rval, frame = vc.read()
#    if(c%timeF == 0): #每隔timeF帧进行存储操作
#        cv2.imwrite('smallVideo/smallVideo'+str(c) + '.jpg', frame) #存储为图像
    if rval:
        #img为当前目录下新建的文件夹
        cv2.imwrite('img/'+str(c) + '.jpg', frame) #存储为图像
        cv2.waitKey(1)
    else:
        break
vc.release()

1.ERROR :

"mMovieWriter.status: 3. Error: Cannot Save"

是由于已经有了同名文件,无法覆盖,删掉再转换就可以了。

你可能感兴趣的:(python前行者)