|
|
|
|
|
""" |
|
|
Script to extract frames from WebP files in data/shots/ and save them as png files in data/expanded/ |
|
|
|
|
|
Input: data/shots/sample-XXX-Y.webp |
|
|
Output: data/expanded/sample-XXX-Y-Z.png (where Z is the frame number starting from 0) |
|
|
""" |
|
|
|
|
|
import os |
|
|
import sys |
|
|
from pathlib import Path |
|
|
from PIL import Image |
|
|
import re |
|
|
|
|
|
|
|
|
def extract_frames_from_webp(input_path, output_dir): |
|
|
""" |
|
|
Extract all frames from a WebP file and save them as png files. |
|
|
|
|
|
Args: |
|
|
input_path (Path): Path to the input WebP file |
|
|
output_dir (Path): Directory to save the extracted frames |
|
|
|
|
|
Returns: |
|
|
int: Number of frames extracted |
|
|
""" |
|
|
try: |
|
|
|
|
|
with Image.open(input_path) as img: |
|
|
|
|
|
filename = input_path.stem |
|
|
match = re.match(r'sample-(\d+)-(\d+)', filename) |
|
|
if not match: |
|
|
print(f"Warning: Filename {filename} doesn't match expected pattern") |
|
|
return 0 |
|
|
|
|
|
sample_num = match.group(1) |
|
|
shot_num = match.group(2) |
|
|
|
|
|
frame_count = 0 |
|
|
|
|
|
|
|
|
try: |
|
|
while True: |
|
|
|
|
|
frame = img.convert('RGB') |
|
|
|
|
|
|
|
|
output_filename = f"sample-{sample_num}-{shot_num}-f{frame_count}.png" |
|
|
output_path = output_dir / output_filename |
|
|
|
|
|
|
|
|
frame.save(output_path) |
|
|
print(f"Extracted frame {frame_count}: {output_filename}") |
|
|
|
|
|
frame_count += 1 |
|
|
|
|
|
|
|
|
img.seek(img.tell() + 1) |
|
|
|
|
|
except EOFError: |
|
|
|
|
|
pass |
|
|
|
|
|
|
|
|
if frame_count == 0: |
|
|
frame = img.convert('RGB') |
|
|
output_filename = f"sample-{sample_num}-{shot_num}-0.jpg" |
|
|
output_path = output_dir / output_filename |
|
|
frame.save(output_path, 'JPEG', quality=95) |
|
|
print(f"Extracted single frame: {output_filename}") |
|
|
frame_count = 1 |
|
|
|
|
|
return frame_count |
|
|
|
|
|
except Exception as e: |
|
|
print(f"Error processing {input_path}: {e}") |
|
|
return 0 |
|
|
|
|
|
|
|
|
def main(): |
|
|
"""Main function to process all WebP files in data/shots/""" |
|
|
|
|
|
|
|
|
script_dir = Path(__file__).parent |
|
|
shots_dir = script_dir / "data" / "shots" |
|
|
expanded_dir = script_dir / "data" / "expanded" |
|
|
|
|
|
|
|
|
if not shots_dir.exists(): |
|
|
print(f"Error: Shots directory not found: {shots_dir}") |
|
|
sys.exit(1) |
|
|
|
|
|
|
|
|
expanded_dir.mkdir(exist_ok=True) |
|
|
|
|
|
|
|
|
webp_files = list(shots_dir.glob('sample-193-*.webp')) |
|
|
|
|
|
if not webp_files: |
|
|
print(f"No WebP files found in {shots_dir}") |
|
|
sys.exit(1) |
|
|
|
|
|
print(f"Found {len(webp_files)} WebP files to process") |
|
|
|
|
|
total_frames = 0 |
|
|
processed_files = 0 |
|
|
|
|
|
|
|
|
for webp_file in sorted(webp_files): |
|
|
print(f"\nProcessing: {webp_file.name}") |
|
|
frames_extracted = extract_frames_from_webp(webp_file, expanded_dir) |
|
|
|
|
|
if frames_extracted > 0: |
|
|
total_frames += frames_extracted |
|
|
processed_files += 1 |
|
|
else: |
|
|
print(f"Failed to extract frames from {webp_file.name}") |
|
|
|
|
|
print(f"\n=== Summary ===") |
|
|
print(f"Processed files: {processed_files}/{len(webp_files)}") |
|
|
print(f"Total frames extracted: {total_frames}") |
|
|
print(f"Output directory: {expanded_dir}") |
|
|
|
|
|
|
|
|
if __name__ == "__main__": |
|
|
main() |