-2

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"
3
  • Please post an input image by itself. Commented Sep 12, 2024 at 15:43
  • @fmw42 I included an image in the "example" link (SO says I can't embed yet). But I've ended up creating another question just for the colour index part, as the solution I was asking for was not going to work out anyway. Commented Sep 12, 2024 at 16:03
  • I was asking you to provide the input image without the resulting image append to it so that one can process just the input image alone. Commented Sep 12, 2024 at 16:04

2 Answers 2

0

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

Sign up to request clarification or add additional context in comments.

1 Comment

Remap seems best, but I don't have access to a list of "desired" colours either. I've come up with another solution where I isolate the mask in a separate file instead. For anyone searching for this question though, this is a good answer.
0

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

2 Comments

Huh? Is this the answer? If so, please show the images and code so other folks can understand and use it. If it isn't the answer, it's in the wrong place.
This is more of an abstract question, not about a specific piece of code. The entire scope of this operation would take several pages to explain, and would only add confusion. The answer isn't answering the question directly (hence why I picked another as the accepted one), but it still provides some useful perspective on whether the requested solution is even worth pursuing. If this explanation still isn't to your satisfaction, I apologise, but that's all the energy I have reserved for StackOverflow nitpickery.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.