Removing all but one color

Questions and postings pertaining to the usage of ImageMagick regardless of the interface. This includes the command-line utilities, as well as the C and C++ APIs. Usage questions are like "How do I use ImageMagick to create drop shadows?".
Post Reply
rwielinga

Removing all but one color

Post by rwielinga »

Hello all,

I am fairly new to using the ImageMagick software. I am looking to perform a rather simple task, but after digging through the forum for a couple of hours and trying out several options I have not succeeded in reaching my goal.

My goal:

- I have an image with several colors
- I want to select all pixels of a specific color in the original image (result may be black and white)
- Must be possible to perform this in batch mode

This thread has given me some pointers, but my brain does not cooperate with me understanding it ;)
viewtopic.php?f=10&t=8711

I think the approach should be to create a mask which highlights only the pixels of the specific color (lets say 'red').
I have tried to indicate that 'red' should be the transparency color and then create a mask from that using the matte channel:

From the samples:
convert drawn.png -channel matte -separate +matte matte.png

But my attempts were far from successful.

Hmm, update...! Found this post:
viewtopic.php?p=21394#p21394
convert colorwheel.jpg -matte \
\( +clone -fuzz 30% -transparent blue \) \
-compose DstOut -composite all_blue_colors.png

Which suggests to make all pixels not near the same color of 'red' (in my example) transparent. Although this works, it uses the -fuzz attribute, but I only want to select ONE color. (changing the -fuzz to 1 does not seem to work, 2% does seem to work, but I think too many colors will fall within the range).

My question is:
- Am i on the right track?
- Is there an easier way to do this?
- Or... How do I do this!!! ;)

I am a bit lost at this point... Any help greatly appreciated!
rwielinga

Re: Removing all but one color

Post by rwielinga »

Best way to solve your problem is by formulating your problem... I seem to have been able to do it after all:

convert step0.png -matte ( +clone -fuzz 1 -transparent #8787b4 ) -compose DstOut -composite step1.png

The remaining steps should not be too difficult (making the remaining pixels black instead of the original color).


Thanks for your help ;)
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: Removing all but one color

Post by anthony »

Replace the non-transparent pixels with black...

Code: Select all

   -fill black -colorize 100%
replace transparency with white

Code: Select all

  -background white +compose -flatten
I added the +compose to turn off the previous -compose switch.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
Post Reply