忽略循环中的某些元组

2024-03-28 09:58:56 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在生成一些元组中的动画帧,其中可能包含(0,0)值,我希望在使用moviepy生成帧时忽略这些值。xy在循环中包含以下内容。第一次和第二次迭代包含(0,0),其余包含浮点。我想忽略那些拥有(0,0)的迭代。可以用moviepy的make_frame(t)def完成吗?我需要一些建议。你知道吗

(0, 0)
(0, 0)
(82.5, 82.5)
(82.5, 82.5)
(108.28125, 108.28125)
(108.28125, 108.28125)

我收到以下错误:

Traceback (most recent call last):
  File "C:\vhosts\VIDEO_TWO_CLONE\vapory-examples-master\scene9.py", line 151, in <module>
    clip = VideoClip(make_frame, duration=5)
  File "C:\Anaconda64\lib\site-packages\moviepy\video\VideoClip.py", line 103, in __init__
    self.size = self.get_frame(0).shape[:2][::-1]
AttributeError: 'NoneType' object has no attribute 'shape'
Process terminated with an exit code of 1

以下是make_帧(t)的定义:

def make_frame(t):

    # PREPARE A DRAWING SURFACE
    surface = gizeh.Surface(width=W, height=H, bg_color=(0,0,0)) # in pixels

    p = PointAnimation((0,0), (110,110), tween=['easeOutElastic', 1, 0.2])

    xy = p.make_frame(t, 0.2, 1, 4, 5)

    if str(xy) == '(0,0)':
        circle = gizeh.circle(r=30, xy=xy, fill=(1,1,0))
        circle.draw(surface) # draw the circle on the surface
        return surface.get_npimage()


clip = VideoClip(make_frame, duration=5)
clip.write_gif("circle.gif", fps=25, fuzz=10) 

Tags: inpyselfclipmakedeflinesurface
1条回答
网友
1楼 · 发布于 2024-03-28 09:58:56

在您的代码中,有if str(xy) == '(0,0)',而实际上这应该是if str(xy) == '(0, 0)'(注意元组中的空格;这是元组在Python中转换为字符串的方式)。然而,一个更好的方法是if xy == (0,0)(这里的空格并不重要,因为没有到字符串的转换)。你知道吗

相关问题 更多 >