使用ImageMagick将动态gif图像的背景改为透明,但帧重叠了。如何解决?

4
我想将gif背景更改为透明,我使用了ImageMagick的以下命令:

convert input.gif -transparent white output.gif

然而,输出结果存在幽灵效应,后续帧与其前一帧重叠。
//很抱歉,我没有足够的声望来发布自己的图片。 一个示例在这里:http://tieba.baidu.com/p/1090763568 如何使它们不重叠?或者换句话说,没有幽灵效应?

1
尝试在输入图像名称后立即添加“-set dispose background”。 - Mark Setchell
2个回答

2

我认为这个"答案"是,它是为你的目的而制作不良的动画。你可以使用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

enter image description here

如果您尝试使用“背景”方法将帧拼回去,那么只出现在第一帧中的单词“welcome”将会丢失:

convert -dispose background frame-0* a.gif

enter image description here

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

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

enter image description here

我唯一能想到的解决方法是将第一帧导入到Photoshop中,然后移除男孩和女孩,并重新构建动画...但是,我不是动画方面的专家,现在所有的帧都已经“摆在桌面上”,其他人也许能告诉你更简单的方法。


1

如果你使用Gimp进行编辑,有一个选项可以更改处理方法为替换(“未指定帧处置:每个图层一个帧(替换)”,这样每个帧在完全清除前一个帧之后呈现)。

但是,如果您不想逐个文件进行编辑,可以使用另一个程序,我已经创建了一个Ruby脚本来修复这个问题(如果需要,批量修复是可能的)。 我找到的唯一方法是对ImageMagick使用的处置方法进行二进制替换,将其替换为Gimp使用的方法值(“Replace”,9:据我所知,这是唯一适用于具有透明背景的动画gif的方法)。注意:要运行脚本,您需要安装ruby(如果您没有安装),并编辑您希望工作的文件名称。

Ruby脚本以将gif的处置方法更改为“替换”

脚本的描述(包括问题和示例动画的描述,重叠(有缺陷)和替换(可用))


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接