Using ImageMagick to change background of an anima

2019-03-31 11:48发布

问题:

I want to change the gif background into transparent, and I use the following command of ImageMagick:

convert input.gif -transparent white output.gif

However, the output has ghosting effect, the later frames overlapped with its previous frames.

//Sorry, I don't have enouth reputation to post my own images. A example is here: http://tieba.baidu.com/p/1090763568

How can I make them do not overlap? Or in other words, no ghosting effect?

回答1:

I think the "answer" is that it is a badly made animation for your purposes. You can see that the animation consists of 12 frames like this with ImageMagick:

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

You can split the image into its constituent frames and make the white areas transparent like this:

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

You can also montage all the frames together like this:

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

If you try to put the frames back together with a disposal method of background, you will lose the word welcome which only appears in the first frame:

convert -dispose background frame-0* a.gif

If you try and composite the first frame (so you have "welcome") with each of the others before animating them, you will get a black ghost because the boy and girl characters also appear in the first frame:

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

The only solution I see is to take the first frame into Photoshop and remove the boy and girl, then rebuild the animation... but then again, I am no expert in animation and now that all the frames are "on the table", someone else may be able to tell you an easier method.



回答2:

If you edit it with Gimp, there's an option to change the disposal method to replace ("Frame disposal where unspecified: One frame per layer (replace)", so that each frame is rendered after fully clearing the previous one).

However, if you don't want to edit each file, one at a time, using another program, I've created a ruby script to fix this for me (batch fix is possible if you need it). The only way I've found is to do a binary replacement of the disposal methods used by ImageMagick to the value of the method used by Gimp ("Replace", 9: the only one that works on animated gifs with transparent background as far as I know). Note: to run the script you would have to install ruby (if you don't have it) and edit the file names you wish to work upon.

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).