-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmake_gif.py
54 lines (41 loc) · 1.73 KB
/
make_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
import os
import cv2
import fire
import numpy as np
from PIL import Image
from tqdm import tqdm
def make_gif(frame_paths, output_path):
master_image = Image.open(frame_paths.pop(0))
images = [Image.open(path) for path in frame_paths]
frames = []
for image in images:
frame = Image.new("RGB", master_image.size)
width_diff = master_image.size[0] - image.size[0]
frame.paste(image, (width_diff // 2, 0))
frames.append(frame)
master_image.save(
output_path, save_all=True, append_images=frames, loop=0, duration=1000
)
def make_video(frame_paths, output_path, frame_repeat=1):
size = Image.open(frame_paths[0]).size
fourcc = cv2.VideoWriter_fourcc(*"mp4v") # (*'avc1')
# fourcc = cv2.cv.CV_FOURCC(*'XVID')
video = cv2.VideoWriter(output_path, fourcc, 60, size)
for path in tqdm(frame_paths):
image = Image.open(path)
frame = Image.new("RGB", size)
width_diff = size[0] - image.size[0]
frame.paste(image, (width_diff // 2, 0))
npframe = np.array(frame)
for i in range(frame_repeat):
video.write(cv2.cvtColor(npframe, cv2.COLOR_RGB2BGR))
video.release()
def video_from_dir(directory, output="./animated.mp4", frame_repeat=1):
pics = os.listdir(directory)
frames = sorted([os.path.join(directory, pic) for pic in pics])
make_video(frames, output, frame_repeat=frame_repeat)
if __name__ == "__main__":
fire.Fire(video_from_dir)
# # make_gif(["broadway_tower.jpg", "output/shrinkage/broadway_tower_0080_shrunk.png"], "output/comparison.gif")
# frames = [f"output/out_{str(i//2).zfill(4)}_{'seamed' if i % 2 == 0 else 'shrunk'}.png" for i in range(2000)]
# make_video(frames, "output/comparison.mp4")