Negate (invert) only black and white?

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
imek
Posts: 7
Joined: 2013-12-05T06:28:44-07:00
Authentication code: 6789

Negate (invert) only black and white?

Post 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:
Image Image
Last edited by imek on 2013-12-05T12:10:35-07:00, edited 1 time in total.
User avatar
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?

Post 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:

Image

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
Image
imek
Posts: 7
Joined: 2013-12-05T06:28:44-07:00
Authentication code: 6789

Re: Negate (invert) only black and white?

Post by imek »

Your suggestion produces this; and by stripping out blur, I get:
Image Image

It's not bad, but we're not quite there yet either. How come the negative of white didn't come out black?
User avatar
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?

Post 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
imek
Posts: 7
Joined: 2013-12-05T06:28:44-07:00
Authentication code: 6789

Re: Negate (invert) only black and white?

Post 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
Post Reply