1
votes

Using ImageMagick, how can I replace everything except a specific list of colors with transparency.

For example, with the source image:

enter image description here

I want to replace everything except cyan, blue, and magenta with transparency (preferably with some fuzz factor):

enter image description here

I've tried +opaque "#0000ff" but that only works with a single color at a time. Any other approach?

1

1 Answers

2
votes

With ImageMagick you can use the plus "+" form of the "-transparent" operator to make everything transparent except the specified color. Run it one time with each of your selected colors on clones of your input, delete the original input, flatten the three remaining images, and write the output.

convert input.png -background none -fill none -fuzz 5% \
   \( -clone 0 +transparent "#ff00ff" \) \
   \( -clone 0 +transparent "#0000ff" \) \
   \( -clone 0 +transparent "#00ffff" \) \
      -delete 0 -flatten result.png