File size: 1,435 Bytes
cfe992b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import gradio as gr
import os
import cv2
from PIL import Image
import tempfile

def images_to_video(image_files, fps):
    temp_dir = tempfile.mkdtemp()
    image_paths = []

    # Save uploaded images to temp folder
    for idx, file in enumerate(image_files):
        img = Image.open(file.name).convert("RGB")
        img_path = os.path.join(temp_dir, f"img_{idx:03d}.png")
        img.save(img_path)
        image_paths.append(img_path)

    if not image_paths:
        return "No images uploaded!"

    # Get size from first image
    first_image = cv2.imread(image_paths[0])
    height, width, _ = first_image.shape

    video_path = os.path.join(temp_dir, "output_video.mp4")
    fourcc = cv2.VideoWriter_fourcc(*'mp4v')
    video = cv2.VideoWriter(video_path, fourcc, fps, (width, height))

    for path in image_paths:
        img = cv2.imread(path)
        video.write(img)

    video.release()
    return video_path

# Gradio interface
iface = gr.Interface(
    fn=images_to_video,
    inputs=[
        gr.File(file_types=["image"], file_count="multiple", label="Upload Images"),
        gr.Slider(minimum=1, maximum=60, value=10, label="FPS (frames per second)")
    ],
    outputs=gr.Video(label="Generated Video"),
    title="🖼️➡️🎥 Image to Video Converter",
    description="Upload images and turn them into a video. Because who has time for manual editing?"
)

if __name__ == "__main__":
    iface.launch()