【问题标题】:Using ImageMagick to change background of an animated gif into transparent, but frames overlapped. How to solve?使用 ImageMagick 将动画 gif 的背景更改为透明,但帧重叠。怎么解决?
【发布时间】:2014-06-10 02:32:15
【问题描述】:

我想把gif背景变成透明,我用ImageMagick的如下命令:

将 input.gif 转换为透明的白色 output.gif

但是,输出有重影效果,后面的帧与前面的帧重叠。

//对不起,我没有足够的声誉来发布我自己的图片。 一个例子在这里:http://tieba.baidu.com/p/1090763568

我怎样才能让它们不重叠?或者换句话说,没有重影效果?

【问题讨论】:

  • 尝试在输入图片名称后立即添加-set dispose background

标签: image imagemagick gif transparent


【解决方案1】:

我认为 “答案” 是为了你的目的,它是一个糟糕的动画。使用 ImageMagick 可以看到动画由 12 帧组成:

identify anim.gif

anim.gif[0] GIF 400x350 400x350+0+0 8-bit sRGB 256c 106KB 0.000u 0:00.000
anim.gif[1] GIF 1x1 400x350+0+0 8-bit sRGB 256c 106KB 0.000u 0:00.000
anim.gif[2] GIF 226x152 400x350+85+8 8-bit sRGB 256c 106KB 0.000u 0:00.000
anim.gif[3] GIF 218x152 400x350+89+8 8-bit sRGB 256c 106KB 0.000u 0:00.000
anim.gif[4] GIF 210x152 400x350+93+8 8-bit sRGB 256c 106KB 0.000u 0:00.000
anim.gif[5] GIF 202x152 400x350+97+8 8-bit sRGB 256c 106KB 0.000u 0:00.000
anim.gif[6] GIF 194x152 400x350+101+8 8-bit sRGB 256c 106KB 0.000u 0:00.000
anim.gif[7] GIF 194x152 400x350+101+8 8-bit sRGB 256c 106KB 0.000u 0:00.000
anim.gif[8] GIF 202x152 400x350+97+8 8-bit sRGB 256c 106KB 0.000u 0:00.000
anim.gif[9] GIF 210x152 400x350+93+8 8-bit sRGB 256c 106KB 0.000u 0:00.000
anim.gif[10] GIF 218x152 400x350+89+8 8-bit sRGB 256c 106KB 0.000u 0:00.000
anim.gif[11] GIF 226x152 400x350+85+8 8-bit sRGB 256c 106KB 0.000u 0:00.000

您可以将图像拆分为其组成帧,并使白色区域透明,如下所示:

convert anim.gif -transparent none frame-%02d.gif

-rw-r--r--   1 mark  staff      9777  4 May 11:11 frame-11.gif
-rw-r--r--   1 mark  staff      9732  4 May 11:11 frame-10.gif
-rw-r--r--   1 mark  staff      9681  4 May 11:11 frame-09.gif
-rw-r--r--   1 mark  staff      9835  4 May 11:11 frame-08.gif
-rw-r--r--   1 mark  staff      9533  4 May 11:11 frame-07.gif
-rw-r--r--   1 mark  staff      9479  4 May 11:11 frame-06.gif
-rw-r--r--   1 mark  staff      9986  4 May 11:11 frame-05.gif
-rw-r--r--   1 mark  staff      9907  4 May 11:11 frame-04.gif
-rw-r--r--   1 mark  staff     10070  4 May 11:11 frame-03.gif
-rw-r--r--@  1 mark  staff     10099  4 May 11:11 frame-02.gif
-rw-r--r--   1 mark  staff        43  4 May 11:11 frame-01.gif
-rw-r--r--@  1 mark  staff     15917  4 May 11:11 frame-00.gif

您也可以像这样将所有帧蒙太奇:

convert anim.gif -transparent white miff:- | montage -tile 3x -frame 5 - montage.jpg

如果你尝试用background的处理方法将帧重新组合在一起,你会丢失只出现在第一帧的welcome这个词:

convert -dispose background frame-0* a.gif

如果您在制作动画之前尝试将第一帧与其他每一帧合成(因此您有“欢迎”),您会得到一个黑色幽灵,因为男孩和女孩角色也出现在第一帧中:

convert frame-00.gif frame-02.gif -geometry +85+8 -composite x.gif

我看到的唯一解决方案是将第一帧放入 Photoshop 中并删除男孩和女孩,然后重建动画......但话又说回来,我不是动画专家,现在所有帧都是 “在桌子上”,别人也许能告诉你一个更简单的方法。

【讨论】:

    【解决方案2】:

    如果您使用 Gimp 对其进行编辑,则可以选择将处置方法更改为替换(“未指定的帧处置:每层一帧(替换)”,以便在完全清除前一帧后渲染每一帧)。

    但是,如果您不想一次编辑每个文件,使用另一个程序,我已经创建了一个 ruby​​ 脚本来为我解决这个问题(如果您需要,可以批量修复)。 我发现的唯一方法是将 ImageMagick 使用的处理方法二进制替换为 Gimp 使用的方法的值(“替换”,9:唯一适用于具有透明背景的动画 gif 的方法)我知道)。注意:要运行脚本,您必须安装 ruby​​(如果您没有它)并编辑您希望使用的文件名。

    Ruby script to change gif's disposal method to "replace".

    Script's description (with a description of the problem and sample animations, overlapping (buggy) and replacing (working).

    【讨论】:

      猜你喜欢
      • 2023-03-03
      • 2021-03-19
      • 1970-01-01
      • 2020-01-24
      • 1970-01-01
      • 1970-01-01
      • 2020-03-18
      • 2012-08-24
      • 1970-01-01
      相关资源
      最近更新 更多