Fill background in animated 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?".
Smoshy
Posts: 14
Joined: 2013-10-05T18:16:01-07:00
Authentication code: 6789

Fill background in animated gif?

Post by Smoshy »

I have a sequence of .pngs, which comprise a blue background with an actor somewhere in the middle. I want to make this into a compact .gif, showing just the actor. I'm using this command line :

Code: Select all

convert r:\in.*.png -set delay 1x24 -coalesce -trim -set dispose background -layers TrimBounds r:\out.gif
Which is almost perfect, but for one problem. Each frame of the gif that is smaller than the gif itself, is filled with a black background. How can I fill that with a plain background colour of my choice?

I bring pictures.
Image

How do I deal with the black blocks? They vex me so.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Fill background in animated gif?

Post by fmw42 »

perhaps you need to trim twice (with -fuzz XX%), once for the white and once for the black. and do a -layers optimize
Smoshy
Posts: 14
Joined: 2013-10-05T18:16:01-07:00
Authentication code: 6789

Re: Fill background in animated gif?

Post by Smoshy »

fmw42 wrote:perhaps you need to trim twice (with -fuzz XX%), once for the white and once for the black. and do a -layers optimize
YES!

I SHALL NAME MY FIRSTBORN fmw42!
Smoshy
Posts: 14
Joined: 2013-10-05T18:16:01-07:00
Authentication code: 6789

Re: Fill background in animated gif?

Post by Smoshy »

fmw42 wrote:perhaps you need to trim twice (with -fuzz XX%), once for the white and once for the black. and do a -layers optimize
Wait, no.

First, the white is an artifact of the screengrab, I'm not worried about that.

I followed your advice, and it works if I use '-background blue' or '-background red'. Any other colour leaves horrible black trails as the animation changes size. Still, it's a lead.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Fill background in animated gif?

Post by fmw42 »

that may (or may not) be an issue with your IM version. what version and platform are you using?

do your images have transparency?
Smoshy
Posts: 14
Joined: 2013-10-05T18:16:01-07:00
Authentication code: 6789

Re: Fill background in animated gif?

Post by Smoshy »

fmw42 wrote:that may (or may not) be an issue with your IM version. what version and platform are you using?
Windows 7 x64

Code: Select all

Version: ImageMagick 6.8.6-10 2013-09-17 Q16 http://www.imagemagick.org
Copyright: Copyright (C) 1999-2013 ImageMagick Studio LLC
Features: DPC OpenMP
Delegates: bzlib freetype jbig jng jp2 jpeg lcms png ps png tiff webp xml zlib
fmw42 wrote:do your images have transparency?
No, they're all opaque .pngs.

Here's what I mean; I used '-background khaki'.
Image
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Fill background in animated gif?

Post by fmw42 »

Include -fuzz XX% and keep increasing the percent until it cleans all the black. Your black is likely not pure black.
Smoshy
Posts: 14
Joined: 2013-10-05T18:16:01-07:00
Authentication code: 6789

Re: Fill background in animated gif?

Post by Smoshy »

Any black is being put there by convert - my source frames have a blue background.
Image

Adding fuzz

Code: Select all

convert r:\in.*.png -set delay 1x24 -coalesce -fuzz 50% -trim -set dispose background -colors 256 +dither -layers TrimBounds -background khaki -layers optimize r:\out.gif
gives
Image

How can I take a pile of images with one background colour and convert them into an animation with the same background colour?
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Fill background in animated gif?

Post by fmw42 »

this works just fine for me on one image in IM 6.8.7.0 Q16 Mac OSX

convert LgZBweF.png -fuzz 50% -trim -colors 256 +dither -layers trimbounds -background khaki -layers optimize test.gif
Smoshy
Posts: 14
Joined: 2013-10-05T18:16:01-07:00
Authentication code: 6789

Re: Fill background in animated gif?

Post by Smoshy »

Not for me...

Image

Can I upload attachments here? A small zip of a few source frames might be helpful...
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Fill background in animated gif?

Post by fmw42 »

You will need to upload to some free image hosting service such as dropbox and then put links to them here. There is no direct image upload in this forum
Smoshy
Posts: 14
Joined: 2013-10-05T18:16:01-07:00
Authentication code: 6789

Re: Fill background in animated gif?

Post by Smoshy »

http://www.4shared.com/zip/GR-Kpf-y/frames.html

The first 24 frames. Thanks for your help on this.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Fill background in animated gif?

Post by fmw42 »

Unfortunately I cannot download from there. It wants a password for logging in and I do not want to sign up just for this.
Smoshy
Posts: 14
Joined: 2013-10-05T18:16:01-07:00
Authentication code: 6789

Re: Fill background in animated gif?

Post by Smoshy »

Sorry, I didn't check to see if there would be any restrictions. I have checked this, and it should be fine : http://wikisend.com/download/686428/frames.zip
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Fill background in animated gif?

Post by fmw42 »

I am not an expert on animation disposal, but I am not sure you can change the background for -dispose background. I think you need to add a frame at the beginning with -dispose none -delay 0 and then process your images with -dispose previous. The problem with that approach is that you use -trim and that will trim the whole first image away.

see the examples on -dispose previous at http://www.imagemagick.org/Usage/anim_basics/#previous

I gave it my best try but was unsuccessful working around this.

Perhaps some one else who is better at animations can provide further insight or wait and see what Anthony suggests.
Post Reply