-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgifsplitter.py
59 lines (51 loc) · 1.9 KB
/
gifsplitter.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
from PIL import Image
import os
import sys
# Get the folder path where the gifs are located
folder_path = rf"{sys.argv[1]}" # To be CLI callable
# Get list of gifs in the folder
gif_files = [f for f in os.listdir(folder_path) if f.endswith('.gif')]
# Iterate through all gifs
for gif_file in gif_files:
gif_path = os.path.join(folder_path, gif_file)
gif = Image.open(gif_path)
# Get gif frames
frames = []
for i in range(0, gif.n_frames):
gif.seek(i)
frames.append(gif.copy())
# Check if the number of frames is over 46
if len(frames) > 46:
i = len(frames)-1
while len(frames) != 46:
if i < 0:
break
frames.pop(i)
i -= 2
# Create individual output folder for each gif
gif_name = os.path.splitext(gif_file)[0]
gif_output_folder = os.path.join(folder_path, gif_name)
if not os.path.exists(gif_output_folder):
os.makedirs(gif_output_folder)
# Save frames to output folder
print(gif_output_folder)
for i in range(len(frames)):
frames[i].save(os.path.join(gif_output_folder, f'frame_{i}.png'))
num_sequence = " ".join(str(i) for i in range(gif.n_frames))
print(num_sequence)
with open(os.path.join(gif_output_folder,'meta.txt'), 'w') as file:
file.write('Filetype: Flipper Animation\n')
file.write('Version: 1\n\n')
file.write('Width: 128\n')
file.write('Height: 64\n')
file.write(f'Passive frames: {gif.n_frames}\n')
file.write('Active frames: 0\n')
file.write(f'Frames order: {num_sequence}\n')
file.write('Active cycles: 0\n')
file.write('Frame rate: 7\n')
file.write('Duration: 3600\n')
file.write('Active cooldown: 0\n\n')
file.write('Bubble slots: 0\n')
print(f'{gif_name} completed')
# Confirmation message
print("Process completed!")