您的位置:首页 > 健康 > 美食 > moviepy实现9宫格的场景

moviepy实现9宫格的场景

2024/10/6 8:28:52 来源:https://blog.csdn.net/xiaolixi199311/article/details/140309692  浏览:    关键词:moviepy实现9宫格的场景

1. 问题描述和解答

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

2. 代码实现

import math
import random
import uuid
from moviepy.editor import *def center_image(file_name, width, height, scale, pos_x, pos_y):w = (width - pos_x * width * 2) / scaleh = (height - pos_y * height * 2) / scalex = pos_x * width / (2 * scale)y = pos_y * height / (2 * scale)x = math.ceil(x) + random.randint(10, 20)   # 放置计算误差导致x、y偏小y = math.ceil(y) + random.randint(10, 20)new_w = 6 * x + 3 * wnew_h = 6 * y + 3 * hcrop_w = int(new_w / 2 - width / 2)crop_h = int(new_h / 2 - height / 2)clip1 = ImageClip(file_name)clip_w, clip_h = clip1.sizeratio = clip_w / wclip1 = clip1.fx(vfx.resize, newsize=(w, h))clip1 = clip1.fx(vfx.margin, left=x, right=x,top=y, bottom=y,color=(255, 255, 255), opacity=0.0)clip2 = clip1.fx(vfx.mirror_x)clip3 = clip1.fx(vfx.mirror_y)clip31 = clip3.fx(vfx.mirror_x)final_clip = clips_array([[clip31, clip3, clip31],[clip2, clip1, clip2],[clip31, clip3, clip31]])print(final_clip.size)final_clip = final_clip.fx(vfx.crop, x1=crop_w, y1=crop_h, width=width, height=height)save_file = str(uuid.uuid4()) + '.png'final_clip.save_frame(save_file, withmask=True)return final_clip, ratioif __name__ == '__main__':def size(t, total_duration, start_scale, end_scale):if start_scale == end_scale:return end_scaleif t < total_duration:relative_time = t / total_durationscale = start_scale + relative_time * (end_scale - start_scale)else:scale = end_scalereturn scalein_scale = 0.75pos_x, pos_y = (1 - in_scale) / 2, (1 - in_scale) / 2scale = 2.0width = 1080height = 1440s = '/Users/admin/an.jpg'final_clip, ratio = center_image(s, width, height, scale, pos_x, pos_y)final_clip = final_clip.to_ImageClip()duration = 3ic = ImageClip("/Users/admin/2.jpg",duration=duration)ic = ic.resize((width, height))final_clip = final_clip.set_duration(duration)final_clip = final_clip.resize(lambda t: size(t, duration, 1.0, scale))final_clip = final_clip.set_position(('center', 'center'))cvc = CompositeVideoClip([ic, final_clip])cvc.write_videofile("r1.mp4", fps=24)

3. 结果和素材

在这里插入图片描述

liu

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com