-
Notifications
You must be signed in to change notification settings - Fork 1
/
convert_to_video.py
48 lines (33 loc) · 1.17 KB
/
convert_to_video.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
import numpy as np
import binvox_rw
import matplotlib.pyplot as plt
import matplotlib.animation as animation
import os
from moviepy.editor import *
path = "./data/model.binvox"
def func(path, gif_path, video_path):
with open(path, "rb") as f:
model = binvox_rw.read_as_3d_array(f)
voxels = model.data
voxels = np.transpose(voxels, (2, 1, 0))
voxels = np.transpose(voxels, (0, 2, 1))
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
ax.set_axis_off()
for spine in ax.spines.values():
spine.set_visible(False)
ax.tick_params(bottom=False, left=False)
ax.voxels(voxels, edgecolor='k')
def rotate(angle):
ax.view_init(azim=angle)
# 创建动画
rot_animation = animation.FuncAnimation(fig, rotate, frames=np.arange(0, 360, 4), interval=0.5)
# 保存动画
rot_animation.save(gif_path, dpi=100, writer='imagemagick')
video = VideoFileClip(gif_path)
target_duration = video.duration / 3
compressed_video = video.fx(vfx.speedx, target_duration)
compressed_video.write_videofile(video_path)
plt.show()
if __name__ == "__main__":
func(path, "./example.gif", "./example.mp4")