参考博客:https://blog.csdn.net/islinyoubiao/article/details/104978563
直接放代码吧
import cv2
import imageio
def read_video(video_path):
video_cap = cv2.VideoCapture(video_path)
frame_count = 0
all_frames = []
while True:
ret, frame = video_cap.read()
if ret is False:
break
frame = frame[..., ::-1] # opencv读取BGR,转成RGB
all_frames.append(frame)
cv2.imshow('frame', frame)
cv2.waitKey(1)
frame_count += 1
print(frame_count)
video_cap.release()
cv2.destroyAllWindows()
print('===>', len(all_frames))
return all_frames
def frame_to_gif(frame_list):
gif = imageio.mimsave('./result.gif', frame_list, 'GIF', duration=0.00085)
# duration 表示图片间隔
if __name__ == "__main__":
frame_list = read_video('demo.mp4')
frame_to_gif(frame_list)
如果是一系列图片存成 gif ,就直接将图片读到列表里,然后再调用 frame_to_gif
函数就可以了~