【问题标题】:Wand creates empty frame and irregular frame rate魔杖创建空帧和不规则帧速率
【发布时间】:2018-05-17 12:02:08
【问题描述】:

我在 sn-p 下面创建了一个 gif。这个 sn-p 来自提示 here。 sn-p 创建一个空帧,帧之间有不规则的延迟。注意虚无和第一帧之间的延迟,然后第二帧很快出现。为什么会发生这种情况?

我必须事先创建特定尺寸的图像,因为魔杖没有根据最大尺寸序列调整画布大小。我也必须想办法解决这个问题。

def render(self):

        # map anim gif
        wand = Image2(width=205,height=223)

        #create frames
        for each_frame in self._tree_dot_frames:
            img = Image2(blob=each_frame)
            print('Image width: {} height: {}'.format(img.width,img.height))
            wand.sequence.append(img)

        #set frame rate
        for cursor in range(len(self._tree_dot_frames)):
            with wand.sequence[cursor] as frame:
                print('sequence width: {} height: {}'.format(frame.width,frame.height))
                frame.delay = 100

        #wand.merge_layers('merge')
        wand.format = 'gif'
        wand.save(filename='animated.gif')
        print('No of frames: {} gif width: {} height: {}'.format(len(wand.sequence),wand.width,wand.height))
        self._tree_dot_blob = wand.make_blob()

        return self._tree_dot_blob

输出:
图片宽度:175 高度:136
图片宽度:205 高度:223
序列宽度:205 高度:223
序列宽度:175 高度:136
帧数:3 gif 宽度:205 高度:223

PS:我还有一个类似 sn-p here 的问题,不要与这个问题混淆。

更新 1:
如果我创建没有预定义大小的魔杖对象,以下是输出。动画看起来不错,但画布是第一个序列(较小),所以最终的 gif 被裁剪,现在完全显示第二个序列。

    #wand = Image2(width=205,height=223) 
    wand = Image2()

更新 2:
如果进行上述更改,以下是输出,我还反向输入序列,这样现在魔杖将采用第一个序列大小。现在最终的 gif 大小还可以,但是如您所见,较大的帧总是留在背景中,这是现在的新问题。

    #create frames
    for each_frame in reversed(self._tree_dot_frames):
        img = Image2(blob=each_frame)
        print('Image width: {} height: {}'.format(img.width,img.height))
        wand.sequence.append(img)

如果我在上述任何方向上都会卡住,请提供帮助。

所需的解决方案将采用最大尺寸的馈送帧,并容纳该尺寸的所有帧/类似下面的事情应该做(我在上面的问题中只显示了第一 2 帧数据)

【问题讨论】:

    标签: python imagemagick wand


    【解决方案1】:

    根据解决方案here 中给出的提示,我想出了以下修改后的 sn-p 似乎可以解决问题。显然,我的要求需要 MagickSetImageDispose 或 MagickExtentImage 尚未在 wand 中实现。我非常感谢emcconville 的帮助,如果需要以下解决方案,请他添加他的 cmets。我还在探索。如果对此问题有任何进一步的问题,我会回复。

    def render(self):
    
        wand = Image2()        
    
        #get max frame height, width
        frameSizeList = []
        tempImgList = []
        for each_frame in self._tree_dot_frames:
            img = Image2(blob=each_frame)
            frameSizeList.append((img.width,img.height))
            tempImgList.append(img)
        optimalFrameSize = (max(frameSizeList,key=itemgetter(1)))
        #print('Max Frame size detected: ',optimalFrameSize)
    
        #create frames
        for each_frame in tempImgList:
            newImg = Image2(width=optimalFrameSize[0], height=optimalFrameSize[1], background=Color('WHITE'))
            newImg.composite(each_frame,0,0)
            wand.sequence.append(newImg)
    
        #set frame rate
        for cursor in range(len(self._tree_dot_frames)):
            with wand.sequence[cursor] as frame:
                print('sequence width: {} height: {}'.format(frame.width,frame.height))
                frame.delay = 100
    
        #wand.merge_layers('merge')
        wand.format = 'gif'
        wand.save(filename='animated.gif')
        print('No of frames: {} gif width: {} height: {}'.format(len(wand.sequence),wand.width,wand.height))
        self._tree_dot_blob = wand.make_blob()
    
        return self._tree_dot_blob
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-05-31
      • 2010-12-17
      • 2015-12-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多