I’m trying to reduce the number of colors in a png while ignoring the alpha channel to produce results similar to what I’d get in Photoshop or GIMP. I’ve read the ImageMagick quantize page, and tried out the various options it goes over to little success. ImageMagick continues to mess with the transparency even when I use the TransparentColorspace (-transparent) when quantizing.
Results similar to the second image are what I’m aiming for, which is what the first looks like after reducing the colors to 4 in GIMP (the transparency channel is preserved in the end result and didn’t affect the resulting colors from quantization). The third and fourth are what ImageMagick produces with RGB and Transparent colorspace settings (large sections of the opaque areas are converted to transparent, and the 4th color is used for transparency; resulting colors are darker overall).
2
Answers
Working off of Mark Setchenll's answer, I noticed his command strips the transparency and replaces it with a color based on the image's contents but increases the color count. Working from that, I found that overlaying the original image over the result allowed me to then reduce the color count without the resulting colors being noticeably different because of the transparency. The original alpha channel is then copied over from the original image to produce a result that is very similar to what is produced by GIMP.
The final reduction is one larger to account for transparency, and produces a color palette similar in count and colors to those from image editors for this image.
Comparison Images
convert UmRGOCs.png -separate -channel RGB -colors 4 +channel -combine out.png
convert emblem.png ( +clone ( +clone -separate -channel RGB +dither -colors 4 +channel -combine ) +swap -flatten ) -channel A -threshold 50% +swap -compose CopyOpacity -composite +dither -colors 5 test.png
Splitting the channels and explicitly selecting the channels for which to reduce the colours, then re-combining channels, might be a way to go: