5
1
Using GraphicsMagick, it's fairly easy to replace one colour with transparency...
gm convert -transparent magenta src.png dst.png
Unfortunately, I can't figure out a simple way to do the exact opposite. If I have an image file with alpha-channel transparency, how do I compose it on a constant-colour background then discard the now redundant alpha channel? The background colour should affect pixels that were partially transparent as well as fully transparent.
I've spotted the +matte option, but it literally discards the alpha layer - normally leaving junk colour pixels where the transparency was, rather than e.g. using the -background option to specify a background colour to use.
The only way I've managed this so far is using multiple calls - one to generate a solid-colour rectangle image file, another to composite the original image with the solid-colour image, and a third to discard the alpha channel. This seems crazy - surely it should be possible with a single fairly simple call?
I currently use GraphicsMagick, but there's no commitment - I'll switch to ImageMagick if it makes things easier.
EDIT
When I say "The background colour should affect pixels that were partially transparent as well as fully transparent.", I mean the result should be visibly what I would normally expect from blitting a transparent sprite onto a solid colour background. The partially transparent pixels should be blended.
This means that if I then reversed the process - turning the background colour key into transparency - I would expect to see a halo around the original image, where the background colour is blended into the pixels so the colour key doesn't precisely match and therefore isn't converted back to transparency. It's not what most people would want for that reason. It's what I want, though, as I won't be converting the background back to transparency - I'll still have the original image with the original transparency if I need it.
Hmmm - a background image constructed as a mosaic of zero images on a solid colour background. Clever. Not tested yet, but I think you'll be getting the accept. – Steve314 – 2010-11-23T05:26:06.180
Sorry - no accept because it didn't work. gm complains that it needs an image list for -mosaic. It only needs one image to work around that, but even by e.g. having the same source image twice, you get a result but it doesn't draw a background of the colour specified under the transparency. I get roughly the same as a simple
gm convert
using only the+matte
option, basically, which also doesn't use-background
even if you specify it - you just get the junk pixels that were previously hidden by the transparency. – Steve314 – 2010-11-23T20:15:04.690Oops, sorry. I've edited my answer with a working solution. I made the mistake of assuming GraphicsMagick and ImageMagick solutions were more interchangeable than they are. – kate – 2010-11-24T03:55:16.830
Success! - or apparent success, anyway. I couldn't find an image with real transparency to test just at the moment, so made one using -transparent, but that doesn't give any semi-transparent pixels. Still, it seems OK for the moment - thanks and accepted. – Steve314 – 2010-11-24T22:44:21.630
I found that the following command works for both GraphicsMagick and ImageMagick (with or without 'gm'):
gm convert -geometry 50x50! -background white -extent 0x0 +matte in.png out.jpg – PowerKiKi – 2012-01-17T05:49:16.160