How can I remove the background from a gif?

Questions and postings pertaining to the usage of ImageMagick regardless of the interface. This includes the command-line utilities, as well as the C and C++ APIs. Usage questions are like "How do I use ImageMagick to create drop shadows?".
Post Reply
bobhwantstoknow
Posts: 8
Joined: 2016-02-23T14:17:23-07:00
Authentication code: 1151

How can I remove the background from a gif?

Post 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
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

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

Post 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.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

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

Post 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
bobhwantstoknow
Posts: 8
Joined: 2016-02-23T14:17:23-07:00
Authentication code: 1151

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

Post 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
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

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

Post 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
bobhwantstoknow
Posts: 8
Joined: 2016-02-23T14:17:23-07:00
Authentication code: 1151

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

Post by bobhwantstoknow »

I was afraid that might be the case. Thanks for the clippingmagic suggestion.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

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

Post by fmw42 »

I posted a few other links, one is from OpenCV for grabcut.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

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

Post by snibgo »

If the background doesn't change between frames, you can use that to find it.
snibgo's IM pages: im.snibgo.com
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

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

Post 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.
appy
Posts: 1
Joined: 2018-11-06T12:18:31-07:00
Authentication code: 1152

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

Post 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.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

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

Post by fmw42 »

Try this:

Code: Select all

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