-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathzoom_effect.py
60 lines (43 loc) · 1.52 KB
/
zoom_effect.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
60
import moviepy.editor as mp
import math
from PIL import Image
import numpy
def zoom_in_effect(clip, zoom_ratio=0.04):
def effect(get_frame, t):
img = Image.fromarray(get_frame(t))
base_size = img.size
new_size = [
math.ceil(img.size[0] * (1 + (zoom_ratio * t))),
math.ceil(img.size[1] * (1 + (zoom_ratio * t)))
]
# The new dimensions must be even.
new_size[0] = new_size[0] + (new_size[0] % 2)
new_size[1] = new_size[1] + (new_size[1] % 2)
img = img.resize(new_size, Image.LANCZOS)
x = math.ceil((new_size[0] - base_size[0]) / 2)
y = math.ceil((new_size[1] - base_size[1]) / 2)
img = img.crop([
x, y, new_size[0] - x, new_size[1] - y
]).resize(base_size, Image.LANCZOS)
result = numpy.array(img)
img.close()
return result
return clip.fl(effect)
def clip_from_image(image_path, duration, size):
image_video = mp.ImageClip(image_path).set_fps(24).set_duration(duration).resize(size)
clip = zoom_in_effect(image_video, 0.01)
return clip
if __name__ == "__main__":
images = [
'out-0.png',
]
slides = []
for n, url in enumerate(images):
slides.append(clip_from_image(url, 25))
# slides.append(
# mp.ImageClip(url).set_fps(25).set_duration(25).resize(size)
# )
#
# slides[n] = zoom_in_effect(slides[n], 0.01)
video = mp.concatenate_videoclips(slides)
video.write_videofile('zoomin.mp4')