-
Notifications
You must be signed in to change notification settings - Fork 4
/
animate_frames.py
45 lines (36 loc) · 1.21 KB
/
animate_frames.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
#!/usr/bin/env python
import math
import argument
import imageio
import numpy as np
import tqdm
from PIL import Image
def zoom_at(org_img, zoom, x, y):
w, h = org_img.size
xz = x / zoom
yz = y / zoom
box = (int(x - xz), int(y - yz), int(x - xz + w / zoom), int(y - yz + h / zoom))
img = org_img.crop(box)
img = img.resize((w, h), Image.LANCZOS)
return img
@argument.entrypoint
def main(*,
frame_count: int = 38,
target: str = 'dalle',
frames: int = 40,
fps: float = 30):
log_scale = math.log(1.82)
with imageio.get_writer(target + ".mp4", mode='I', fps=fps) as writer:
for i in range(frame_count, 0, -1):
img = Image.open(target + '/frame' + str(i) + ".png")
w, h = img.size
for frame in tqdm.tqdm(range(frames)):
if i == 1 and frame == frames // 2:
break
zoom = math.exp(log_scale * frame / (frames))
zoomed = zoom_at(img, zoom, w // 2 + 64, h // 2 + 64)
w, h = zoomed.size
zoomed = zoomed.crop((16, 16, w - 32, h - 32))
writer.append_data(np.array(zoomed))
if __name__ == '__main__':
main()