(venv) daddy@DESKTOP-PQLPS4F:/mnt/c/Users/Jeff/logo$ python3 3d.py
Traceback (most recent call last):
File "/mnt/c/Users/Jeff/logo/3d.py", line 93, in
create_3d_rotation_logo(image_path)
File "/mnt/c/Users/Jeff/logo/3d.py", line 63, in create_3d_rotation_logo
animated_logo = create_rotation_animation(logo_clip, duration=base_duration)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/mnt/c/Users/Jeff/logo/3d.py", line 32, in create_rotation_animation
scaled = ImageClip(make_frame=custom_make_frame, duration=duration)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^import numpy as np
from moviepy.editor import ImageClip, CompositeVideoClip
from moviepy.video.fx import all as vfx
from PIL import Image
import os
def create_rotation_animation(clip, duration=4):
"""Create a smooth rotation animation suitable for a landscape design logo."""
code
w, h = clip.size
# Create smooth 360-degree rotation for looping
rotated = (clip
.fx(vfx.rotate,
lambda t: 360 * t / duration, # Complete 360° rotation
expand=True))
# Instead of using resize fx, we'll implement our own scaling
def custom_make_frame(t):
frame = rotated.get_frame(t)
scale = 1 + 0.08 * np.sin(2 * np.pi * t / duration)
# Convert to PIL Image for high-quality resizing
pil_img = Image.fromarray(frame)
new_size = tuple(int(dim * scale) for dim in pil_img.size)
resized = pil_img.resize(new_size, Image.Resampling.LANCZOS)
# Convert back to numpy array
return np.array(resized)
# Create new clip with the custom frame maker
scaled = ImageClip(make_frame=custom_make_frame, duration=duration)
scaled = scaled.set_position('center')
return scaled
def create_3d_rotation_logo(image_path, output_path=None):
"""Create a seamless looping 3D rotation animation."""
# Set default output path
if output_path is None:
output_dir = os.path.dirname(image_path)
output_path = os.path.join(output_dir, "rotating_platinum_logo.mp4")
code
Assistant
# Load and prepare the image
logo = Image.open(image_path).convert("RGBA")
# Calculate size to fit within 1024x1024 while maintaining aspect ratio
max_size = 900 # Larger initial size for business logo
ratio = min(max_size/logo.size[0], max_size/logo.size[1])
new_size = tuple(int(dim * ratio) for dim in logo.size)
logo = logo.resize(new_size, Image.Resampling.LANCZOS)
# Convert to numpy array
logo_array = np.array(logo)
# Create base clip
base_duration = 4 # Duration for one complete rotation
logo_clip = (ImageClip(logo_array)
.set_duration(base_duration)
.set_position('center'))
# Apply rotation animation
animated_logo = create_rotation_animation(logo_clip, duration=base_duration)
# Create the main clip with exact 1024x1024 dimensions
main_clip = (CompositeVideoClip(
[animated_logo],
size=(1024, 1024),
bg_color=None) # None ensures transparency
.set_duration(base_duration))
# Write video with transparency support and loop
main_clip.write_videofile(
output_path,
fps=30,
codec='libx264',
preset='medium',
ffmpeg_params=[
'-vf', 'format=yuva420p',
'-colorspace', 'bt709',
'-color_primaries', 'bt709',
'-color_trc', 'bt709',
'-pix_fmt', 'yuva420p', # Ensure alpha channel support
'-movflags', 'faststart',
'-shortest',
'-filter_complex', '[0:v]loop=999:32767:0[out]', # Add looping
'-map', '[out]'
]
)
if name == "main":
image_path = "/mnt/c/Users/Jeff/logo/logo.png"
create_3d_rotation_logo(image_path)
TypeError: ImageClip.init() got an unexpected keyword argument 'make_frame'
The error you're encountering stems from the misuse of the ImageClip class in MoviePy. Specifically, ImageClip does not accept a make_frame argument, which is intended for the VideoClip class.
Here's a detailed explanation and the steps to resolve the issue:
Understanding the Error
Error Message:
code
TypeError: ImageClip.__init__() got an unexpected keyword argument 'make_frame'
Cause:
The ImageClip class is designed to handle static images. Its constructor expects an image (as a path or an array) and does not handle frame-by-frame generation via the make_frame function.
On the other hand, VideoClip is designed for creating video clips where each frame can be dynamically generated using the make_frame function.
Solution
To fix the error, you should replace ImageClip with VideoClip when you intend to create a clip using a make_frame function. Below are the steps to modify your script accordingly.
1. Import the Correct Class
First, import VideoClip from moviepy.video.VideoClip:
python
from moviepy.video.VideoClip import VideoClip
You can also import it alongside your existing imports:
python
from moviepy.editor import ImageClip, CompositeVideoClip
from moviepy.video.fx import all as vfx
from moviepy.video.VideoClip import VideoClip # Add this line
from PIL import Image
import numpy as np
import os
2. Replace ImageClip with VideoClip in create_rotation_animation
In your create_rotation_animation function, replace the ImageClip constructor with VideoClip:
python
def create_rotation_animation(clip, duration=4):
"""Create a smooth rotation animation suitable for a landscape design logo."""
w, h = clip.size
# Create smooth 360-degree rotation for looping
rotated = (clip
.fx(vfx.rotate,
lambda t: 360 * t / duration, # Complete 360° rotation
expand=True))
# Instead of using resize fx, we'll implement our own scaling
def custom_make_frame(t):
frame = rotated.get_frame(t)
scale = 1 + 0.08 * np.sin(2 * np.pi * t / duration)
# Convert to PIL Image for high-quality resizing
pil_img = Image.fromarray(frame)
new_size = tuple(int(dim * scale) for dim in pil_img.size)
resized = pil_img.resize(new_size, Image.Resampling.LANCZOS)
# Convert back to numpy array
return np.array(resized)
# Create new clip with the custom frame maker using VideoClip
scaled = VideoClip(make_frame=custom_make_frame, duration=duration)
scaled = scaled.set_position('center')
return scaled
3. Verify Other Parts of the Code
Ensure that other parts of your script use ImageClip appropriately. For instance, loading and setting up the static logo image should still use ImageClip as it's meant for static images:
Here's the corrected version of your script with the necessary changes:
python
Additional Recommendations
Check MoviePy Version:
Ensure that your MoviePy installation is up to date. You can check the version using:
bash
pip show moviepy
To update MoviePy, run:
bash
pip install --upgrade moviepy
Handling Transparency:
If you intend to maintain transparency in your output video, ensure that both the input images and the output settings correctly handle alpha channels. Note that not all codecs support alpha channels. The libx264 codec with yuva420p pixel format is generally a good choice, but verify compatibility based on your requirements.
Performance Optimization:
Using VideoClip with a custom make_frame function can be computationally intensive, especially for high-resolution videos. If you encounter performance issues:
Consider reducing the resolution.
Optimize the custom_make_frame function for efficiency.
Use more efficient libraries for image processing if necessary.
Looping the Video:
The ffmpeg_params you've set include a filter to loop the video. Ensure that this aligns with your desired output. The parameters you've used (loop=999:32767:0) are intended to loop the video multiple times, but you can adjust these values based on how long you want the final video to be.
Conclusion
By replacing ImageClip with VideoClip when using the make_frame function, your script should work as intended, creating a smooth 3D rotation animation of your logo. Ensure all other parts of the script are compatible with this change and test the output to verify the results.
If you continue to experience issues or need further assistance, feel free to ask!