John
John

Reputation: 31

How to filter 4 different colours on ImageMagick?

I'm using ImageMagick to transform an image into x and y cartesian co-ordinates. The code that I am using in the command line does fine to make all the lines appear as fully black and the blank spaces appear as fully white. Is there a way to also filter for more colours? For example, if the image consists of multiple colours, is there a way to make all the red-type colours fully red, all the blue-type colours fully blue and the other non-white colours as black? Then the background would just be seen as fully white (most images I am using as a picture on a white background). The command line text is:

convert "imagename".png -white-threshold 50% -black-threshold 50% txt:

Using Mark Setchell's code from below, I tried to convert this basic image that I created using paint to test the abilities of IM.

enter image description here

I edited the code slightly so that the background fill would be white instead of black as such;

convert q.png -write MPR:orig -delete 0 -compose lighten ^ ( MPR:orig -fuzz 10% -fill red  -opaque red  -fill white +opaque red  ) ^ ( MPR:orig -fuzz 10% -fill blue -opaque blue -fill white +opaque blue ) -composite ^ ( MPR:orig -fuzz 10% -fill lime -opaque lime -fill white +opaque lime ) -composite ^ f.png

However, the entire image just shows white. Any tips?

Upvotes: 0

Views: 1366

Answers (1)

Mark Setchell
Mark Setchell

Reputation: 207670

Not exactly sure what you want, but maybe this will get us closer. So, we start with this:

enter image description here

And we then do this, which says "make anything within 10% of red into solid red, anything within 25% of blue into solid blue, and so on":

convert start.png  -fuzz 10%  \
   -fill red   -opaque red    \
   -fuzz 25%                  \
   -fill blue  -opaque blue   \
   -fill lime  -opaque lime   \
   -fill white -opaque white result.png

enter image description here


Or maybe more like this:

convert start.png -write MPR:orig -delete 0 -compose lighten \
   \( MPR:orig -fuzz 10% -fill red  -opaque red  -fill black +opaque red \) \
   \( MPR:orig -fuzz 20% -fill blue -opaque blue -fill black +opaque blue \) -composite \
   \( MPR:orig -fuzz 30% -fill lime -opaque lime -fill black +opaque lime \) -composite \
   result.png

enter image description here

That says... "Take the original image and make a copy in the Magick Persistent Register called orig then delete the original. Set the blending mode to select the lightest pixel at each location for any future composite commands. Now make a new layer, fill it with the original image, make anything within 10% of red into solid red and make anything else black. Now do the same for colours within 20% of blue and blend taking the lighter layer - so the blues and reds show instead of the blacks which will always be darkest. Do the same again for lime - which is how ImageMagick refers to pure green.""

Note how the green is larger than the blue which, in turn, is larger than the red because of the differing amounts of fuzz I applied - i.e. 30%, 20% 10%.


I think that looks like this with Windows-style quoting:

convert start.png -write MPR:orig -delete 0 -compose lighten ^
( MPR:orig -fuzz 10% -fill red  -opaque red  -fill black +opaque red  ) ^
( MPR:orig -fuzz 20% -fill blue -opaque blue -fill black +opaque blue ) -composite ^
( MPR:orig -fuzz 30% -fill lime -opaque lime -fill black +opaque lime ) -composite ^
result.png

Upvotes: 1

Related Questions