Page 1 of 1

How can I remove the background from a gif?

Posted: 2016-05-04T21:08:42-07:00
by bobhwantstoknow
Hello,

I have a gif with a stable static background image and movement in the foreground. Is there an easy way to mask out the movement and remove the background? For example, if I wanted to create my own Travolta gif http://imgur.com/j51uHm1 , or BB8 https://media.giphy.com/media/eEbiAqk9YUT5e/giphy.gif

Re: How can I remove the background from a gif?

Posted: 2016-05-04T21:31:36-07:00
by fmw42
Are these the images you want to remove the background or the result of removing the background. If the former, namely a reasonably constant color background, then just use

Code: Select all

convert image -fuzz XX% -transparent yourbackgroundcolor result
or

Code: Select all

convert image -fuzz XX% -fill none -draw "alpha 0,0 floodfill" result
for each frame. The result will have a transparent background

Please always provide your IM version and platform when asking questions, since the syntax may differ.

Re: How can I remove the background from a gif?

Posted: 2016-05-04T21:35:53-07:00
by fmw42
If it is an animated gif and you want all the frames with tranparent background, then

Code: Select all

convert -delay Y animation.gif -coalesce -fuzz XX% -transparent yourbackgroundcolor -loop X -layers optimize new_animation.gif

Re: How can I remove the background from a gif?

Posted: 2016-05-04T21:45:11-07:00
by bobhwantstoknow
Those are examples of the types of results I would like to achieve by removing the backgrounds from the original videos/gifs.

Version: ImageMagick 6.8.9-9 Q16 i586 2016-02-05

Re: How can I remove the background from a gif?

Posted: 2016-05-04T21:48:09-07:00
by fmw42
Background removal of non-constant background is very difficult. There is no good way that I know besides manually tracing the boundary. There are some tools out there that make it easier, but one still has to outline roughly. See for example:

https://clippingmagic.com/
http://research.microsoft.com/pubs/1474 ... rabcut.pdf
https://graphics.ethz.ch/teaching/forme ... apping.pdf
http://docs.opencv.org/3.1.0/d8/d83/tut ... #gsc.tab=0

Re: How can I remove the background from a gif?

Posted: 2016-05-04T21:55:44-07:00
by bobhwantstoknow
I was afraid that might be the case. Thanks for the clippingmagic suggestion.

Re: How can I remove the background from a gif?

Posted: 2016-05-04T21:56:45-07:00
by fmw42
I posted a few other links, one is from OpenCV for grabcut.

Re: How can I remove the background from a gif?

Posted: 2016-05-04T22:05:18-07:00
by snibgo
If the background doesn't change between frames, you can use that to find it.

Re: How can I remove the background from a gif?

Posted: 2016-05-04T23:12:29-07:00
by fmw42
Adding to what snibgo said, I think he is referring to frame-to-frame differences to see what is changing and what is not, if the background does not change between frames.

By tacking on, you run the risk of the IM developers not seeing your specific question. Yes, all questions are about this vulnerability, but each system and IM version may be different, in general, and need different responses. There have been many questions, each asking a different aspect of the vulnerability.That is why I suggested new topics for each.

Re: How can I remove the background from a gif?

Posted: 2018-11-06T12:24:41-07:00
by appy
This seems to do part of the trick but each previous frame is overlayed:
convert bg-yellowish.gif -fuzz 3% -transparent "#fff5ba" a1.gif

I grabbed the yellowish gif from here:
https://static.ezgif.com/images/bg-yellowish.gif

I am able to do what I need at ezgif.com with the "don't stack frames" option, but I need to do this in code.

Re: How can I remove the background from a gif?

Posted: 2018-11-06T13:31:01-07:00
by fmw42
Try this:

Code: Select all

convert -dispose background bg-yellowish.gif -coalesce -fuzz 3% -transparent "#fff5ba" -layers optimize a1.gif