-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathframe-splitter.py
119 lines (97 loc) · 3.72 KB
/
frame-splitter.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
from PIL import Image
import os
import sys
def extract_frames(input_path):
"""Extract frames from PNG/GIF file"""
img = Image.open(input_path)
frames = []
try:
while True:
# Copy the current frame
frames.append(img.copy())
img.seek(img.tell() + 1)
except EOFError:
pass
return frames
def create_atlas(frames, output_path):
"""Create horizontal atlas from frames"""
if not frames:
print("No frames found in the input file")
return False
frame_width = frames[0].width
frame_height = frames[0].height
# Calculate atlas dimensions
num_frames = len(frames)
atlas_width = num_frames * frame_width
atlas_height = frame_height
# Create new image for atlas
atlas = Image.new('RGBA', (atlas_width, atlas_height), (0, 0, 0, 0))
# Place frames horizontally
for i, frame in enumerate(frames):
atlas.paste(frame, (i * frame_width, 0))
# Create output directory if it doesn't exist
os.makedirs(os.path.dirname(output_path), exist_ok=True)
# Save atlas
atlas.save(output_path)
print(f"Created atlas with {num_frames} frames")
print(f"Atlas dimensions: {atlas_width}x{atlas_height} pixels")
print(f"Saved to: {output_path}")
return True
def process_file(input_path):
"""Process a single file"""
try:
# Generate output path by adding _atlas to the original filename
file_name = os.path.splitext(input_path)[0]
output_path = f"{file_name}_atlas.png"
frames = extract_frames(input_path)
if create_atlas(frames, output_path):
return 1
return 0
except Exception as e:
print(f"Error processing {input_path}: {e}")
return 0
def process_directory(directory):
"""Process all PNG files in a directory"""
if not os.path.exists(directory):
print(f"Error: Directory {directory} not found")
return
# Create output directory if needed
output_dir = os.path.join(directory, "atlas_output")
os.makedirs(output_dir, exist_ok=True)
successful = 0
total = 0
# Process all PNG files in the directory
for root, _, files in os.walk(directory):
for file in files:
if file.lower().endswith('.png'):
total += 1
input_path = os.path.join(root, file)
# Create relative output path
rel_path = os.path.relpath(root, directory)
output_subdir = os.path.join(output_dir, rel_path)
os.makedirs(output_subdir, exist_ok=True)
file_name = os.path.splitext(file)[0]
output_path = os.path.join(output_subdir, f"{file_name}_atlas.png")
try:
frames = extract_frames(input_path)
if create_atlas(frames, output_path):
successful += 1
except Exception as e:
print(f"Error processing {input_path}: {e}")
print(f"\nProcessing complete!")
print(f"Successfully processed {successful} out of {total} PNG files")
print(f"Output directory: {output_dir}")
if __name__ == "__main__":
if len(sys.argv) != 2:
print("Usage: python frame-splitter.py <input_file.png/.gif or directory>")
sys.exit(1)
input_path = sys.argv[1]
# Check if input exists
if not os.path.exists(input_path):
print(f"Error: Path {input_path} not found")
sys.exit(1)
# Process either a single file or a directory
if os.path.isfile(input_path):
process_file(input_path)
else:
process_directory(input_path)