File size: 931 Bytes
434512f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import cv2
import os
import numpy as np
import zipfile
from PIL import Image

def frames_to_video(frames, output_path, fps=10):
    if not frames: return None
    # Ensure dimensions are compatible with most players (multiples of 2)
    width, height = frames[0].size
    fourcc = cv2.VideoWriter_fourcc(*'mp4v')
    out = cv2.VideoWriter(output_path, fourcc, fps, (width, height))
    
    for frame in frames:
        # Convert PIL to NumPy, then RGB to BGR for OpenCV
        frame_numpy = np.array(frame)
        out.write(cv2.cvtColor(frame_numpy, cv2.COLOR_RGB2BGR))
    
    out.release()
    return output_path

def export_to_zip(frames, zip_name="frames.zip"):
    with zipfile.ZipFile(zip_name, 'w') as zipf:
        for i, frame in enumerate(frames):
            frame_path = f"frame_{i:04d}.png"
            frame.save(frame_path)
            zipf.write(frame_path)
            os.remove(frame_path)
    return zip_name