1

Looking for a way to make multiple (In this case 4, but in the future arbitrary) colors in an animated gif transparent with imagemagick. I know I can use multiple -transparent commands, but when the animated gif is composed, it results in some bizarre compositing, with previous frames remaining rendered instead of clearing the canvas. This doesn't happen if just one color is made transparent.

For example, here's the original image:

animated gif 1

and here's the result of my code.

converted animated gif

The 4 background colors were successfully made transparent, but now the animated portions that were directly contacting the transparent areas aren't cleared after every frame.

Here's my script:

/usr/local/bin/convert 'https://radblast.wunderground.com/cgi-bin/radar/WUNIDS_map?num=40&type=N0Q&mapx=400&mapy=240&brand=wui&delay=15&frame=0&scale=1&transx=0&transy=0&severe=0&smooth=0&centerx=400&centery=240&station=GRK&rainsnow=0&lightning=0&noclutter=0&showlabels=1&showstorms=0&rand=27081615' -transparent '#87876F' -transparent '#6C6C58' -transparent '#1C3575' -transparent '#162A5D' ~/Desktop/radar.gif
Lazerr Tag
  • 61
  • 3

2 Answers2

2

This works for me in ImageMagick. You need to specify -dispose background.

convert -dispose background anim.gif -transparent '#87876F' -transparent '#6C6C58' -transparent '#1C3575' -transparent '#162A5D' x.gif

enter image description here

fmw42
  • 46,825
  • 10
  • 62
  • 80
  • That's fantastic. Thank you! I haven't fully worked out all the -dispose options yet, but this seems to do the trick. – Lazerr Tag Jun 29 '21 at 13:08
  • Hmmmm... but now as I'm seeing in your example, it crops each frame to the bounds of the differences in each previous frame. That's weird. I wonder if there's a way to avoid that behavior? – Lazerr Tag Jun 29 '21 at 14:39
  • Sorry, I overlooked that. I tried several things including -coalesce, but that did not help. I think you should post this question on the Imagemagick GIT forum in the issues section at https://github.com/ImageMagick/ImageMagick/issues – fmw42 Jun 29 '21 at 16:22
  • No worries, I actually figured it out! See below. -coalesce worked with the appropriate optimize to recombine frames at the end. Thanks for all your work on ImageMagick, generally. – Lazerr Tag Jun 29 '21 at 17:44
  • Good work. I did the -coalesce but with -layers optimize, which did not work. Nice work figuring out -layers optimizeframe. – fmw42 Jun 29 '21 at 18:26
1

Okay, here's ultimately what worked; splitting the animation into individual frames with -coalesce and recombining them.

convert ~/Desktop/radar.gif -coalesce -transparent '#87876F' -transparent '#6C6C58' -transparent '#1C3575' -transparent '#162A5D' -layers OptimizeFrame ~/Desktop/radar.gif
Lazerr Tag
  • 61
  • 3