import numpy as np import os import time from PIL import Image from moviepy.editor…
- User
import numpy as np import os import time from PIL import Image from moviepy.editor import VideoClip
Paths
image_one_path = '/home/daddy/morph/one.jpeg' image_two_path = '/home/daddy/morph/two.png' output_path = '/home/daddy/morph/output' output_filename = f'morph{int(time.time())}.mp4'
Function to create each frame of the video
def make_frame(t): if t < duration1: # Display the first image frame_array = img1_array.copy() elif t < duration1 + duration2: # Morphing effect with particle explosion and implosion progress = (t - duration1) / duration2 # Normalize progress to [0, 1]
code# Create an empty frame with transparency frame_array = np.zeros_like(img1_array) frame_array[:, :, 3] = 0 # Set alpha channel to 0 (fully transparent) for idx, (x, y) in enumerate(particle_positions): # Extract the patch from the first image patch = img1_array[y:y+particle_size, x:x+particle_size].copy() # Explosion offsets offset_x, offset_y = explosion_offsets[idx] if progress < 0.5: # Explosion phase explosion_progress = progress * 2 # Scale progress to [0, 1] current_x = x + explosion_progress * offset_x current_y = y + explosion_progress * offset_y else: # Implosion phase implosion_progress = (progress - 0.5) * 2 # Scale progress to [0, 1] # Target positions in the second image target_x, target_y = particle_to_target_position.get((x, y), (x, y)) # Current position moves from exploded position to target position current_x = (x + offset_x) + implosion_progress * (target_x - (x + offset_x)) current_y = (y + offset_y) + implosion_progress * (target_y - (y + offset_y)) # Convert positions to integers and ensure they stay within frame boundaries current_x = int(np.clip(current_x, 0, width - particle_size)) current_y = int(np.clip(current_y, 0, height - particle_size)) # Place the current patch onto the frame frame_array[current_y:current_y+particle_size, current_x:current_x+particle_size] = patch else: # Display the second image frame_array = img2_array.copy() # Convert the NumPy array back to an image frame_image = Image.fromarray(frame_array, 'RGBA') # Return frame as an RGB image (MoviePy expects RGB format) return np.array(frame_image.convert('RGB')) - Assistant
It looks like the last line of your code is incomplete. Specifically, you're missing the arguments inside the
os.path.join()function in theanimation.write_videofile()call. Here's the incomplete line:pythonanimation.write_videofile(os.path.join( what is missingTo fix this, you need to provide the path and filename where you want to save the output video. Earlier in your code, you've defined
output_pathandoutput_filename:pythonoutput_path = '/home/daddy/morph/output' output_filename = f'morph{int(time.time())}.mp4'You can use these variables to complete the
os.path.join()function call. Here's the corrected line:python