import os
import cv2
import os
#首先对图像文件排序进行规整
img_sort = os.listdir('D:/2cyclist2video/pic')
# get_key是sotred函数用来比较的元素,该处用lambda表达式替代函数。
get_key = lambda i : int(i.split('.')[0])
img_new_sort = sorted(img_sort, key=get_key)
print(img_sort, '\n', img_new_sort) #将没有规整的和规整之后的文件夹做对比
path = './pic/'
filelist = os.listdir(path)
filelist = sorted(filelist)
filelist =img_new_sort #使用规整后的图像拼成视频
fps = 24 # 视频每秒24帧
size = (1280, 590) # 需要转为视频的图片的尺寸
# 可以使用cv2.resize()进行修改
#video = cv2.VideoWriter("VideoTest2.avi", cv2.VideoWriter_fourcc('I', '4', '2', '0'), fps, size,True)
# 视频保存在当前目录下
fourcc = cv2.VideoWriter_fourcc(*'XVID') #第二次尝试,转为彩色
video = cv2.VideoWriter('testwrite666.avi',fourcc, 20.0, (1280,590),True)
for item in filelist:
if item.endswith('.jpg'):
# 找到路径中所有后缀名为.png的文件,可以更换为.jpg或其它
item = path + item
img = cv2.imread(item)
video.write(img)
video.release()
cv2.destroyAllWindows()
pwd = os.getcwd()
print(pwd)
实现将视频按照任意帧率转化为图片:
# This is a sample Python script.
# Press Shift+F10 to execute it or replace it with your code.
# Press Double Shift to search everywhere for classes, files, tool windows, actions, and settings.
def print_hi(name):
# Use a breakpoint in the code line below to debug your script.
print(f'Hi, {name}') # Press Ctrl+F8 to toggle the breakpoint.
# Press the green button in the gutter to run the script.
if __name__ == '__main__':
# import cv2
#
# cap = cv2.VideoCapture("E:\MyVideo\jcole.mp4") # 名为'003.mp4'的文件
# c = 0 # 文件名从0开始
# while (1):
# # get a frame
# ret, frame = cap.read()
# # show a frame
# cv2.imshow("capture", frame)
# cv2.imwrite('image/' + str(c) + '.jpg', frame) # 存储为图像
#
#
# c = c + 1
# if cv2.waitKey(100) & 0xFF == ord('q'):
# break
# cap.release()
# cv2.destroyAllWindows()
# See PyCharm help at https://www.jetbrains.com/help/pycharm/
import cv2
###视频按帧率显示 10代表10ms一帧就是播放速度2.0.
# cap = cv2.VideoCapture("E:\MyVideo\jcole.mp4") # 名为'003.mp4'的文件
# c = 0 # 文件名从0开始
# while (1):
# # get a frame
# ret, frame = cap.read()
# # show a frame
# cv2.imshow("capture", frame)
# cv2.imwrite('image/' + str(c) + '.jpg', frame) # 存储为图像
# c = c + 1
# if cv2.waitKey(30) & 0xFF == ord('q'):
# break
# cap.release()
# cv2.destroyAllWindows()
# ### 保存为图片
# videoCapture = cv2.VideoCapture()
# videoCapture.open('E:\MyVideo\jcole.mp4')
#
# fps = videoCapture.get(cv2.CAP_PROP_FPS)
# frames = videoCapture.get(cv2.CAP_PROP_FRAME_COUNT)
# # fps是帧率,意思是每一秒刷新图片的数量,frames是一整段视频中总的图片数量。
# print("fps=", fps, "frames=", frames)
#
# for i in range(0,int(frames)*1000,1083100): #按每10000帧截屏一次
# ret, frame = videoCapture.read()
# cv2.imwrite("E:\MyVideo\jcole(%d).jpg" % i, frame)
import cv2
# -*- coding:utf-8 -*-
import cv2
import os
#cap = cv2.VideoCapture(r"D:\aaa训练视频\2cyclist1.mp4")
cap = cv2.VideoCapture(r"D:\CYCLISTvideo\2cyclist2.mp4")
FPS = cap.get(5)
c = 1
# timeRate = 10 # 截取视频帧的时间间隔(这里是每隔10秒截取一帧)
st = '0.01' # 开始时间,等同于0分01秒
ed = '10.35' # 结束时间,等同于10分35秒
start = int(st.split('.')[0]) * 60 + int(st.split('.')[1])
end = int(ed.split('.')[0]) * 60 + int(ed.split('.')[1])
startframe = int(FPS) * start # 因为cap.get(5)获取的帧数不是整数,所以需要取整一下(向下取整用int,四舍五入用round,向上取整需要用math模块的ceil()方法)
endframe = int(FPS) * end
output = r'D:\2cyclist2video'.format(st, ed)
if not os.path.exists(output):
os.makedirs(output)
while (True):
ret, frame = cap.read()
if ret:
if startframe <= c <= endframe and c % 1 == 0: # 我自己自定的是每隔1000帧保存一次
print("开始截取视频第:" + str(c) + " 帧")
# 这里就可以做一些操作了:显示截取的帧图片、保存截取帧到本地
# print(frame.shape)
cv2.imwrite(output + r'\\' + str(c) + '.jpg', frame) # 这里是将截取的图像保存在本地
# cv2.waitKey(0)
if c > endframe:
print('需要的帧已截取')
break
c += 1
else:
print("所有帧都已经保存完成")
break
cap.release()