Artifechs
Artifechs

Reputation: 1

How do I reduce a specific colour range to a single colour?

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"

Upvotes: -1

Views: 79

Answers (2)

fmw42
fmw42

Reputation: 53164

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:

enter image description here

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

enter image description here

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

enter image description here

Upvotes: 0

Artifechs
Artifechs

Reputation: 1

Actually, this method is unfeasible, as it can end up targeting unintended pixels. A much better way is to isolate the mask in a separate file, where it's just the mask and a transparent background, remove the antialiasing with -kmeans 2 and then overlay that image onto the base texture with composite

Upvotes: 0

Related Questions