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:
Negate (invert) only black and white?
Negate (invert) only black and white?
Last edited by imek on 2013-12-05T12:10:35-07:00, edited 1 time in total.
- fmw42
- Posts: 25562
- Joined: 2007-07-02T17:14:51-07:00
- Authentication code: 1152
- Location: Sunnyvale, California, USA
Re: Negate (invert) only black and white?
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
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?
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?
It's not bad, but we're not quite there yet either. How come the negative of white didn't come out black?
- fmw42
- Posts: 25562
- Joined: 2007-07-02T17:14:51-07:00
- Authentication code: 1152
- Location: Sunnyvale, California, USA
Re: Negate (invert) only black and white?
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
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?
Brilliant. Thank you!
For future reference:
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