实现时段截取、矩形窗截取、帧等间隔采样,帧图片缩放。
Python图片转视频和视频转图片以及视频转GIF
csdn
import cv2
import math
file_path=r'C:\Users\li\AppData\Local\ttt\file_example_AVI_480_750kB.avi'
cap = cv2.VideoCapture(file_path) # 加载视频文件
#播放视频,esc建退出:
while cap.isOpened():
ret, frame = cap.read()
if ret:
#frame_gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
#frame_RGB = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB) #open-cv颜色通道BGR,与图片RGB不一样?
cv2.imshow('frame', frame)
else:
print('视频播放完成!')
break
key=cv2.waitKey(10)
if key==27: #按键esc
break
#视频转图片:
cap_num = cap.get(7) # 获取视频总帧数
cap_width = math.ceil(cap.get(3)) # 获取视频帧宽度(横)
cap_height = math.ceil(cap.get(4)) # 获取视频帧高度(竖)
cap_fps = math.ceil(cap.get(5)) # 获取视频帧率
# 得到视频总帧数的位数,比如198帧(三位数),得到3;1989帧(4位数),得到4:
cap_count = 0
while cap_num:
cap_count = cap_count+1
cap_num = math.floor(cap_num/10)
fix = '%0'+str(cap_count)+'d' # 得到图片保存的前缀,比如001.png,0001.png
cap_cnt = 1
ret, frame = cap.read() # 读取图片
while ret:
file_pic_path = 'c:/Users/li/AppData/Local/ttt/pics/'+str(fix%cap_cnt)+'.png' # 图片保存目录
#frame_RGB = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB) #open-cv颜色通道BGR,与图片RGB不一样??
cv2.imwrite(file_pic_path,frame)
cap_cnt = cap_cnt+1
ret, frame = cap.read()
key=cv2.waitKey(10)
if key==27: #按键esc
break
#释放资源:
cap.release()
cv2.destroyAllWindows()
######## 保存为avi视频格式,超占内存,每张图片4M,1000张,完完整整的3.5G #########
import cv2
import os
fourcc = cv2.VideoWriter_fourcc('m','p','4','v') # 设置输出视频为mp4格式
# fourcc = cv2.VideoWriter_fourcc('I','4','2','0') # 设置输出视频为avi格式,很占内存
# cap_fps是帧率,可以根据随意设置;size要和图片的size一样,但是通过img.shape得到图像的参数是
#(height,width,channel),但是此处的size要传的是(width,height),这里一定要注意注意,
# 不然结果会打不开,提示“无法解码多工传送的流”等.比如通过img.shape得到常用的图片尺寸
#(480,270,3),则size设为(480,270)
cap_fps = 30
size = (480, 270)
# 设置输出视频的参数,如果是灰度图,可以加上 isColor = 0 这个参数
#video = cv2.VideoWriter('results/result.avi',fourcc, cap_fps, size, isColor=0)
video = cv2.VideoWriter('result.mp4',fourcc, cap_fps, size)
# 这里直接读取py文件所在目录下的pics目录所有图片。
path = './pics/'
file_lst = os.listdir(path)
for filename in file_lst:
img = cv2.imread(path + filename)
video.write(img)
video.release()
### ======================= 使用imageio包将图片转GIF ======================== ###
import os
import imageio
# 需要转GIF的图片路径
path = './pics/'
img_lst = os.listdir(path)
frames = []
for img in img_lst:
frames.append(imageio.imread(path + img))
# duration是保存一帧图片的时间,其实就是相当于帧率。使用imageio方法来resize图像还没找到
imageio.mimsave("result.gif", frames, 'GIF', duration=0.033)
实现时段截取、矩形窗截取、帧等间隔采样,帧图片缩放。
import cv2
import imageio
cap = cv2.VideoCapture(r'C:\Users\li\AppData\Local\ttt\file_example_AVI_480_750kB.avi')
fps = cap.get(cv2.CAP_PROP_FPS)
width=int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
height=int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
start_time = 20*fps
end_time = 25*fps
image_lst = []
i = 0
GAP=5 #采样间隔每3帧取一个
while True:
ret, frame = cap.read()
if frame is None:
break
if ret == False:
break
if (i>=start_time and i<=end_time and i%GAP==0):
frame=frame[10:height-10,100:width-100] #取矩形窗口的图像
frame = cv2.resize(frame, None, fx=0.5, fy=0.5, interpolation=cv2.INTER_AREA) #图片缩小一倍
frame_rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB) #等同frame_rgb = frame[:,:,(2,1,0)]
image_lst.append(frame_rgb)
cv2.imshow('a', frame)
key = cv2.waitKey(1)
if key == ord('q'):
break
i +=1
cap.release()
cv2.destroyAllWindows()
# Convert to gif using the imageio.mimsave method
imageio.mimsave(r'C:\Users\li\AppData\Local\ttt\result_fromavi.gif', image_lst, fps=fps/GAP)