Spaces:
Sleeping
Sleeping
Add Image-to-Video Animation feature
Browse filesGenerates smooth zoom animation from a single image and outputs MP4 video for sharing.
- image_to_video.py +56 -0
image_to_video.py
ADDED
|
@@ -0,0 +1,56 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import cv2
|
| 3 |
+
import numpy as np
|
| 4 |
+
from moviepy.editor import ImageClip
|
| 5 |
+
import tempfile
|
| 6 |
+
import os
|
| 7 |
+
|
| 8 |
+
def animate_image(image, duration=5, zoom=1.2):
|
| 9 |
+
"""📸 Image -> Smooth Zoom Video"""
|
| 10 |
+
if image is None:
|
| 11 |
+
return None
|
| 12 |
+
|
| 13 |
+
try:
|
| 14 |
+
# Save uploaded image temporarily
|
| 15 |
+
temp_img = tempfile.NamedTemporaryFile(delete=False, suffix=".png")
|
| 16 |
+
cv2.imwrite(temp_img.name, cv2.cvtColor(image, cv2.COLOR_RGB2BGR))
|
| 17 |
+
|
| 18 |
+
# Create animation using MoviePy
|
| 19 |
+
clip = ImageClip(temp_img.name)
|
| 20 |
+
w, h = clip.size
|
| 21 |
+
|
| 22 |
+
def zoom_in(get_frame, t):
|
| 23 |
+
frame = get_frame(t)
|
| 24 |
+
scale = 1 + (zoom - 1) * (t / duration)
|
| 25 |
+
new_w = int(w * scale)
|
| 26 |
+
new_h = int(h * scale)
|
| 27 |
+
frame = cv2.resize(frame, (new_w, new_h))
|
| 28 |
+
x = (new_w - w) // 2
|
| 29 |
+
y = (new_h - h) // 2
|
| 30 |
+
return frame[y:y+h, x:x+w]
|
| 31 |
+
|
| 32 |
+
animated_clip = clip.fl(zoom_in, apply_to=["mask"]).set_duration(duration)
|
| 33 |
+
|
| 34 |
+
# Save video
|
| 35 |
+
temp_video = tempfile.NamedTemporaryFile(delete=False, suffix=".mp4")
|
| 36 |
+
animated_clip.write_videofile(temp_video.name, fps=24, codec="libx264", audio=False, verbose=False, logger=None)
|
| 37 |
+
|
| 38 |
+
return temp_video.name
|
| 39 |
+
except Exception as e:
|
| 40 |
+
print(f"Error: {e}")
|
| 41 |
+
return None
|
| 42 |
+
|
| 43 |
+
# ---------- Gradio UI ----------
|
| 44 |
+
with gr.Blocks() as demo:
|
| 45 |
+
gr.Markdown("<h2 style='text-align:center'>🎬 Image to Video Animation — SaEdit MultiAi</h2>")
|
| 46 |
+
|
| 47 |
+
img_input = gr.Image(type="numpy", label="📷 Upload an image")
|
| 48 |
+
duration_input = gr.Slider(3, 10, value=5, step=1, label="⏳ Duration (seconds)")
|
| 49 |
+
zoom_input = gr.Slider(1.0, 2.0, value=1.2, step=0.1, label="🔍 Zoom Level")
|
| 50 |
+
btn = gr.Button("Generate Animation")
|
| 51 |
+
video_output = gr.Video(label="🎞️ Animated Video")
|
| 52 |
+
|
| 53 |
+
btn.click(animate_image, inputs=[img_input, duration_input, zoom_input], outputs=video_output)
|
| 54 |
+
|
| 55 |
+
if __name__ == "__main__":
|
| 56 |
+
demo.launch()
|