Page 1 of 1
Negate (invert) only black and white?
Posted: 2013-12-05T06:33:01-07:00
by imek
Can ImageMagick negate only non-saturated colors (black, white, and gray)?
I tried negating the whole image before rotating the hue, but that didn't produce the expected result.
My image, and the expected result:
Re: Negate (invert) only black and white?
Posted: 2013-12-05T11:03:29-07:00
by fmw42
create a mask by thresholding the saturation channel of colorspace HSB version of the image. Value in the saturation at or near zero are unsaturated such as black/white/gray.
convert image -colorspace HSB -channel g -separate +channel -threshold 0% mask
then create a negated version of the original image.
Then composite the original, then negated image and the mask to get only the grays negated. You may need to swap the first two images to correspond to the polarity of the mask or negate the mask.
If you post a link to your input image, we can try to test with it and provide the commands.
Here is an example:
convert zelda3.png \
\( -clone 0 -negate \) \
\( -clone 0 -colorspace HSB -channel g -separate -threshold 15% -negate -blur 0x1 \) \
-compose over -composite zelda3_lowsat_negate.png
Re: Negate (invert) only black and white?
Posted: 2013-12-05T12:15:56-07:00
by imek
Your suggestion produces this; and by stripping out blur, I get:
It's not bad, but we're not quite there yet either. How come the negative of white didn't come out black?
Re: Negate (invert) only black and white?
Posted: 2013-12-05T12:21:27-07:00
by fmw42
Yes that makes sense, since you are working with a graphic and not an picture. If you had provided the original image first, I would not have put the blur in. I presume you adjusted the threshold factor to suit your image. Use the lowest value that works adequately.
I accidentally left out +channel. So the threshold was giving a color result. Try the following now.
convert neg-bw1.png \
\( -clone 0 -negate \) \
\( -clone 0 -colorspace HSB -channel g -separate +channel -threshold 15% -negate \) \
-compose over -composite neg-bw1.png_lowsat_negate.png
Re: Negate (invert) only black and white?
Posted: 2013-12-05T12:31:33-07:00
by imek
Brilliant. Thank you!
For future reference:
Code: Select all
convert neg-bw1.png -colorspace HSB -channel g -separate +channel -threshold 0% mask
Code: Select all
convert neg-bw1.png \( -clone 0 -negate \) \( -clone 0 -colorspace HSB -channel g -separate +channel -threshold 0% -negate \) -compose over -composite neg-bw1.png_lowsat_negate.png