9

Say I have a PNG with transparent pixels, how could I translate them to a solid color?

(Note this is slightly different to this question as I'm looking to replace with an arbitrary color. White is "special" in ImageMagick, as the accepted answer there indicates by the fact it omits to mention white).

Community
  • 1
  • 1
mahemoff
  • 38,430
  • 30
  • 133
  • 196
  • Just so you know, the imagemagick command has excellent help via this flag: `convert --help`. – halfer Jun 07 '12 at 11:22
  • 2
    @halfer It's voluminous, to be sure: convert --help has 293 lines. Whether or not it is excellent depends largely upon how well tutored someone is in image magick's grammar and idioms. – Wayne Conrad Apr 26 '13 at 17:39
  • Possible duplicate of [Replace transparency in PNG images with white background](http://stackoverflow.com/questions/2322750/replace-transparency-in-png-images-with-white-background) – Gary Jun 23 '16 at 18:36

1 Answers1

21

For me, the following works (ImageMagick 6.6.3-1):

convert in.png -background "#FF0000" -flatten out.png

Just swap the red color to whatever suits your needs.

Maehler
  • 5,373
  • 1
  • 36
  • 44
  • 1
    `-flatten` was the key for me. Otherwise the image seemed unchanged. – Matthew Leingang Jun 23 '16 at 20:08
  • 1
    How can this be extended to animated GIFs? As @MatthewLeingang mentions, omitting `-flatten` seems to have no effect (background still transparent). Adding `-flatten` flattens the entire series of images, i.e., there is no animation any more. – bluenote10 Apr 05 '18 at 08:36