Opencv实现Mat转Gif
程序员文章站
2024-03-22 13:35:22
...
发现了神奇的python视频处理库:moviepy
发现了神奇的Gif生成代码:
import os
import cv2 as cv
import moviepy.editor as mpy
import numpy as np
def make_gif(images, fname, duration=2, true_image=True):
def make_frame(t):
try:
x = images[int(len(images)/duration*t)]
except:
x = images[-1]
if true_image:
return x.astype(np.uint8)
else:
return ((x+1)/2*255).astype(np.uint8)
clip = mpy.VideoClip(make_frame, duration=duration)
clip.write_gif(fname, fps = len(images) / duration)
#fps帧率自己调
dir_list = os.listdir('E:\\[4]datasets\\FaceDatasets\\YTF\\YTF_FaceDetect\\Alison_Lohman\\5')
img_list=[]
for i in range(0,len(dir_list)):
img = cv.imread('E:\\[4]datasets\\FaceDatasets\\YTF\\YTF_FaceDetect\\Alison_Lohman\\5\\' + dir_list[i])
img = cv.cvtColor(img, cv.COLOR_BGR2RGB)
img_list.append(img)
make_gif(img_list, 'test.gif', duration = 4)
输入
输出
推荐阅读