-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcreate_gif.py
59 lines (50 loc) · 1.31 KB
/
create_gif.py
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import os
import argparse
import glob
from PIL import Image
from PIL import ImageFile
ImageFile.LOAD_TRUNCATED_IMAGES = True
parser = argparse.ArgumentParser()
parser.add_argument(
'--image_folder',
type=str,
help='',
)
parser.add_argument(
'--video_name',
type=str,
help='',
)
parser.add_argument(
'--out_dir',
type=str,
default='./gifs',
)
parser.add_argument(
'--rate',
type=int,
default=16,
help='',
)
args = parser.parse_args()
print(args)
image_folder = args.image_folder
video_name = args.video_name
out_dir = args.out_dir
fp_out = os.path.join(out_dir, f"{video_name}.gif")
if not os.path.exists(out_dir):
os.makedirs(out_dir)
images = [img for img in os.listdir(image_folder) if img.endswith(".png")]
# images = [img for img in os.listdir(image_folder) if img.endswith(".jpg")]
images = [int(x[:-4]) for x in images]
images.sort()
images = [f"{x}.png" for x in images]
# images = [f"{x}.jpg" for x in images]
print(image_folder)
print(images)
# https://pillow.readthedocs.io/en/stable/handbook/image-file-formats.html#gif
img, *imgs = [Image.open(os.path.join(image_folder, img)) for img in images]
img = img.resize((200,200))
imgs = [im.resize((200,200)) for im in imgs]
img.save(fp=fp_out, format='GIF', append_images=imgs,
save_all=True, duration=32, loop=0)