skip to Main Content

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.

Album of Examples

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


  1. Chosen as BEST ANSWER

    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.

    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
    

    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

    1. Original
    2. GIMP
    3. convert UmRGOCs.png -separate -channel RGB -colors 4 +channel -combine out.png
    4. 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

  2. Splitting the channels and explicitly selecting the channels for which to reduce the colours, then re-combining channels, might be a way to go:

    convert UmRGOCs.png -separate -channel RGB -colors 4 +channel -combine out.png
    

    enter image description here

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search