I'm using -kmeans
to reduce some images from true RGB to 256 colours, for use in Unreal Engine 1. In order to have transparency in those textures, I need to use a magenta mask colour (#ff00ff
).
The problem is that the images I'm converting are antialiased, so they contain steps between the true magenta and other colours, resulting in pink pixels around the masked area. I cannot change the source images to prevent this antialiasing.
My idea is then to reduce a range of magenta-ish colours to true magenta. (example) How would I go about this?
This is my command so far. I also need to place the magenta as the first colour in the index, which this does, but it worsens the quality of the output significantly:
magick "$PNG_FILE" -define kmeans:seed-colors="#ff00ff" -kmeans 255 "$PCX_FILE"
Here are a couple of ways that may do what you want in Imagemagick. Best in the future to provide separate image, not two images appended together.
Input:
The first is to use -fuzz with a large value with -fill magenta -opaque magenta. The fuzz will find values near magenta and change it to magenta. This process reduced your colors below 256. The larger the fuzz, the less colors. But if too large, you might change other colors that are not wanted. See https://imagemagick.org/Usage/color_basics/#fuzz
magick img.png -fuzz 49% -fill "#ff00ff" -opaque "#ff00ff" x.png
The second is to use -remap on just the colors you want in your output. Since the image you provided is two image appended with a white border, I include white along with the magenta and black background colors. This results in 3 colors in the output as expected. See https://imagemagick.org/Usage/quantize/#web_safe
magick img.png \( xc:black xc:"#ff00ff" xc:white +append -write mpr:img +delete \) +dither -remap mpr:img y.png