Spaces:
Running
Running
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() |