from moviepy.editor import VideoFileClip, ImageClip, CompositeVideoClip
from PIL import Image, ImageDraw, ImageFilter
import os
# Paths to your video and images
video_path = "/Users/jitendersingh/Downloads/background.mp4" #1920x1080
image_paths = [
"/Users/jitendersingh/Downloads/myimages/-original-imagvfy8frnaspkg.jpeg", #atleast 800 px
"/Users/jitendersingh/Downloads/myimages/1.png", #atleast 400 px
"/Users/jitendersingh/Downloads/myimages/2.png",#atleast 400 px
"/Users/jitendersingh/Downloads/myimages/3.png",#atleast 400 px
"/Users/jitendersingh/Downloads/myimages/4.png"#atleast 400 px
]
# Directory to save modified images
temp_dir = "/Users/jitendersingh/Downloads/temp_images"
os.makedirs(temp_dir, exist_ok=True)
# Function to add shadow and rounded corners
def add_shadow_with_rounded_corners(image_path, output_path, corner_radius, shadow_size):
img = Image.open(image_path).convert("RGBA")
original_size = img.size
scaled_size = (original_size[0] - 2 * shadow_size, original_size[1] - 2 * shadow_size)
img = img.resize(scaled_size, Image.ANTIALIAS)
mask = Image.new("L", scaled_size, 0)
draw = ImageDraw.Draw(mask)
draw.rounded_rectangle((0, 0, scaled_size[0], scaled_size[1]), radius=corner_radius, fill=255)
rounded_img = Image.new("RGBA", scaled_size)
rounded_img.paste(img, (0, 0), mask=mask)
shadow = Image.new("RGBA", original_size, (0, 0, 0, 0))
draw = ImageDraw.Draw(shadow)
draw.rounded_rectangle(
(shadow_size, shadow_size, original_size[0] - shadow_size, original_size[1] - shadow_size),
radius=corner_radius,
fill=(0, 0, 0, 128)
)
shadow = shadow.filter(ImageFilter.GaussianBlur(radius=shadow_size // 2))
output = Image.new("RGBA", original_size, (0, 0, 0, 0))
output.paste(shadow, (0, 0))
output.paste(rounded_img, (shadow_size, shadow_size), mask=mask)
output.save(output_path, format="PNG")
# Modify images
corner_radius = 50
shadow_size = 50
modified_image_paths = []
for image_path in image_paths:
output_path = os.path.join(temp_dir, os.path.basename(image_path))
add_shadow_with_rounded_corners(image_path, output_path, corner_radius, shadow_size)
modified_image_paths.append(output_path)
# Load the video
background_video = VideoFileClip(video_path)
# Animation properties
images_properties = [
{"size": (800, 800), "direction": "left_to_right", "position": {"x": 100, "y": 250}, "start_time": 0},
{"size": (380, 380), "direction": "right_to_left", "position": {"x": 1000, "y": 250}, "start_time": 0.5},
{"size": (380, 380), "direction": "right_to_left", "position": {"x": 1400, "y": 250}, "start_time": 0.8},
{"size": (380, 380), "direction": "bottom_to_top", "position": {"x": 1000, "y": 650}, "start_time": 1.1},
{"size": (380, 380), "direction": "bottom_to_top", "position": {"x": 1400, "y": 650}, "start_time": 1.3}
]
# Resize function
def resize_image(image, max_width, max_height):
image_width, image_height = image.size
if image_width > max_width or image_height > max_height:
aspect_ratio = image_width / image_height
if image_width > max_width:
new_width = max_width
new_height = int(new_width / aspect_ratio)
else:
new_height = max_height
new_width = int(new_height * aspect_ratio)
image = image.resize(newsize=(new_width, new_height))
return image
# Animation function
def image_animation(image, direction, position, start_time):
def animate(t):
duration = 0.5
t_offset = t - start_time
if direction == "left_to_right":
x_start = -image.size[0]
x_end = position['x']
y = position['y']
x = x_start + (x_end - x_start) * min(t_offset / duration, 1)
elif direction == "right_to_left":
x_start = background_video.w
x_end = position['x']
y = position['y']
x = x_start - (x_start - x_end) * min(t_offset / duration, 1)
elif direction == "top_to_bottom":
y_start = -image.size[1]
y_end = position['y']
x = position['x']
y = y_start + (y_end - y_start) * min(t_offset / duration, 1)
elif direction == "bottom_to_top":
y_start = background_video.h
y_end = position['y']
x = position['x']
y = y_start - (y_start - y_end) * min(t_offset / duration, 1)
if t_offset >= duration:
return position['x'], position['y']
return x, y
return animate
# Create animated images
animated_images = []
for i, image_path in enumerate(modified_image_paths):
image = ImageClip(image_path).set_duration(background_video.duration).set_start(images_properties[i]["start_time"])
resized_image = resize_image(image.copy(), images_properties[i]["size"][0], images_properties[i]["size"][1])
animation = image_animation(
resized_image,
images_properties[i]["direction"],
images_properties[i]["position"],
images_properties[i]["start_time"]
)
animated_image = resized_image.set_position(animation)
animated_images.append(animated_image)
# Combine the video and all animated images
final_video = CompositeVideoClip([background_video] + animated_images)
# Write the output video
output_path = "/Users/jitendersingh/Downloads/output_with_rounded_images.mp4"
final_video.write_videofile(output_path, codec="libx264", fps=30)
print(f"Animation completed! Output saved to: {output_path}")
0 comments:
Post a Comment