gpt4 book ai didi

python - 绕 x 轴旋转图像

转载 作者:太空宇宙 更新时间:2023-11-03 16:02:37 25 4
gpt4 key购买 nike

我需要绕其 x 轴(或 y 轴)旋转图像。我可以使用 avisynth 轻松创建这样的动画,但现在我需要使用 Python 的 moviepy 模块来实现该效果。我可以使用以下脚本轻松旋转图像,但需要一些线索如何以 2D 或 3D 旋转图像。

enter image description here

from moviepy.editor import *

clip = ImageClip('my_image.jpg')
rotated_clip = (clip.add_mask()
.fx(vfx.resize, width=300, height=300)
.fx(vfx.rotate, lambda t: 90*t, expand=False)
.set_duration(5))
final_clip = CompositeVideoClip([rotated_clip.set_pos("center")], size=(800,800), bg_color=3*[255])
final_clip.write_videofile("test.mp4", fps=25, codec="libx264")

这是实际生成该示例图像的 avisynth 脚本。请注意,它确实需要“QUAD”插件。

function stars(clip c, int r) {
c.Overlay(x=rand(c.width),y=rand(c.height),BlankClip(c,width=1,height=1,color=$030301*rand(85)))
(r==0)? last : stars(r-1)
Trim(0,-1).Loop(c.Framecount, 0, 0)
}
width= 800
height=600
length=100000
Tcolor=$000040
Bcolor=$000018

StackVertical(BlankClip(length=length,width=2,height=1,color=TColor,pixel_type="RGB32"),BlankClip(length=length,width=2,height=1,color=BColor)).BilinearResize(width,2*height,src_top=0,src_height=2).Crop(0,height/2,0,-height/2).Stars(width*height/3072)

ImageSource("path_to_image.png", start=0, end=total_time, fps=300, pixel_type="RGB32")
#BlankClip(length=length,FPS=25,width=640,height=480,color=$000018,pixel_type="RGB32")

#ColorBars()
HALFCYCLE=10 # Frames in 1 HALF rotation (spinning clip)
NSPIN = 1 # Number of HALF rotations in spinning clip
NSTILL = 10 # Frames in STILL clip
V = 0.2 # Tilt/Yaw
tim = PI / HALFCYCLE

ScriptClip("""

c=last
t=tim*current_frame
t1x= 0.5 - 0.5 * cos(t) # BOTH Left
t2x= 0.5 + 0.5 * cos(t) # BOTH Right
#
t1y= 0.0 + V * sin(t) # ] both Top's opposite sign
t2y= 0.0 - V * sin(t) # ]
t3y= 1.0 + V * sin(t) # [ both Bottoms opposite sign
t4y= 1.0 - V * sin(t) # [
ResetMask
quad(t1x,t1y, t2x,t2y, t2x,t3y, t1x,t4y, normal=true)
#Overlay(c,last,mask=last.ShowAlpha())


""")

SPIN=Trim(0,-(NSPIN*HALFCYCLE +1)) # Spinning clip, + 1 to complete last spin
STILL=SPIN.Trim(SPIN.FrameCount-1,-1).Loop(NSTILL,0,0)
SPIN2=Trim((NSPIN%2 ==0)?0:HALFCYCLE,-(NSPIN*HALFCYCLE +1))
SPIN ++ STILL ++ SPIN2
Return Last

最佳答案

实现此目的的一种方法是使用 Vapory,这是 MoviePy 作者的另一个库,它有助于通过 Python 操作 POV-Ray。您可以在 3D 场景中创建一个矩形,并将其绕您喜欢的任何轴旋转,以一定的间隔将帧保存到 MoviePy 剪辑中。

MoviePy + Vapory 代码

from moviepy.editor import concatenate, ImageClip, VideoClip
from vapory import *

img_path = './baseball.png'
img_clip = ImageClip(img_path)
W, H = img_clip.w, img_clip.h
AR = 1.0*W/H

# Set rotation rate by defining the period (in seconds) for 360 deg. revolution
t_rev = 2.0

t_half = t_rev/2.0 # The time required for a half revolution
t_still = 0.8 # How long (in seconds) to hold the half rotated image still


# Static POV-Ray objects
cam = Camera('location', [ 0, 0, -1],
'look_at', [ 0, 0, 0])
light = LightSource([0, 0, -1]) # Light at camera location
bg = Background('color', [0, 0, 0]) # Black background


def scene(t):
""" Returns the scene at time 't' (in seconds) """
s = Scene(camera = cam, objects = [light, bg])

# Add POV-Ray box with image textured on it
s = s.add_objects([
Box([0, 0, 0],
[W, H, 0],
Texture(Pigment(ImageMap('"{}"'.format(img_path), 'once')),
Finish('ambient', 1.0)),
'translate', [-0.5, -0.5, 0],
'scale', [AR, 1, 0],
'rotate', [0, (360/t_rev)*t, 0])]) # Can change axis of rotation here
return s


def make_frame(t):
return scene(t).render(width=W, height=H, antialiasing=0.1)


still_1 = VideoClip(make_frame).to_ImageClip(t=0).set_duration(t_still)
half_1 = VideoClip(make_frame).subclip(0, t_half)
still_2 = VideoClip(make_frame).to_ImageClip(t=t_half).set_duration(t_still)
half_2 = VideoClip(make_frame).subclip(t_half, t_rev)

final_clip = concatenate([still_1, half_1, still_2, half_2])
final_clip.write_gif("./baseball_rot.gif", fps=15)

输出GIF

enter image description here

其他想法:

您可能想要更改的主要内容是 img_patht_rev(完整 360 度旋转的时间)、t_still 和输出帧速率。

我从示例图像中删除了一列像素,以使其宽度均匀(150 像素)。如果您只想制作 GIF,这并不重要,但如果您想制作 x264 编码的 MP4,则可能应该使用 mod2 尺寸。

使用光线追踪器来解决这个问题似乎有点矫枉过正,但这是我想出的第一个有效解决方案。我想将图像表示为 3D 场景中的 2D 矩形,其中我可以简单地指定旋转角度,3D 库将处理其余的事情。

应该可以使用 scikit-image 的投影变换来解决这个问题,如 this MoviePy example 所示。 。请特别注意,该代码 list 中间附近的 trapzWarp 函数。

关于python - 绕 x 轴旋转图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40186787/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com