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.
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.