File size: 2,209 Bytes
681ddc0
 
 
 
5507b97
 
681ddc0
5507b97
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
681ddc0
5507b97
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
681ddc0
5507b97
 
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
import streamlit as st
import cv2
import numpy as np
from PIL import Image
import tempfile
import os

def create_simple_video(images, output_path, fps=1):
    """
    Create a simple video from a list of images
    
    Args:
        images (list): List of PIL Image objects
        output_path (str): Path to save the output video
        fps (int): Frames per second
    """
    # Convert PIL images to OpenCV format
    cv_images = [cv2.cvtColor(np.array(img), cv2.COLOR_RGB2BGR) for img in images]
    
    # Get dimensions from first image
    height, width, _ = cv_images[0].shape
    
    # Define video writer
    fourcc = cv2.VideoWriter_fourcc(*'mp4v')
    out = cv2.VideoWriter(output_path, fourcc, fps, (width, height))
    
    # Write frames
    for img in cv_images:
        out.write(img)
    
    out.release()

def main():
    st.title("🖼️ Image to Simple Video Converter")
    
    # Sidebar for configuration
    st.sidebar.header("Video Settings")
    fps = st.sidebar.slider("Frames per Second", min_value=1, max_value=5, value=1)
    
    # File uploader
    uploaded_files = st.file_uploader(
        "Upload Images", 
        type=['png', 'jpg', 'jpeg', 'webp'], 
        accept_multiple_files=True
    )
    
    if uploaded_files:
        # Sort images to maintain order
        uploaded_files.sort(key=lambda x: x.name)
        
        # Convert uploaded files to PIL Images
        images = [Image.open(file) for file in uploaded_files]
        
        # Create temporary file for video
        with tempfile.NamedTemporaryFile(delete=False, suffix='.mp4') as temp_video:
            video_path = temp_video.name
        
        # Create video
        create_simple_video(images, video_path, fps)
        
        # Display video
        st.video(video_path)
        
        # Provide download button
        with open(video_path, 'rb') as video_file:
            st.download_button(
                label="Download Video",
                data=video_file.read(),
                file_name="converted_video.mp4",
                mime="video/mp4"
            )
        
        # Clean up temporary file
        os.unlink(video_path)

if __name__ == "__main__":
    main()