File size: 1,075 Bytes
a23872f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
import imageio
import glob
import os

def clear_img_dir():
    img_dir = "./img_history"
    if not os.path.exists(img_dir):
        os.mkdir(img_dir)
    for filename in glob.glob(img_dir+"/*"):
        os.remove(filename)
    

def create_gif(total_duration, extend_frames, folder="./img_history", gif_name="face_edit.gif"):
    images = []
    paths = glob.glob(folder + "/*")
    frame_duration = total_duration / len(paths)
    print(len(paths), "frame dur", frame_duration)
    durations = [frame_duration] * len(paths)
    if extend_frames:
        durations [0] = 1.5
        durations [-1] = 3
    for file_name in os.listdir(folder):
        if file_name.endswith('.png'):
            file_path = os.path.join(folder, file_name)
            images.append(imageio.imread(file_path))
    # images[0] = images[0].set_meta_data({'duration': 1})
    # images[-1] = images[-1].set_meta_data({'duration': 1})
    imageio.mimsave(gif_name, images, duration=durations)
    return gif_name

if __name__ == "__main__":
    # clear_img_dir()
    create_gif()
# make_animation()