Convert gray content of gray/white image to black ?

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
Rye
Posts: 158
Joined: 2013-02-25T10:43:05-07:00
Authentication code: 6789

Convert gray content of gray/white image to black ?

Post by Rye »

Well, let's say I have this picture:

Image

Let's assume I want to change all the gray color in this picture to plain black.

Is there a command to have imagemagick change all grey of this image (preferableby using grey [Hex: C8C8C8] to black [Hex: 000000] ?





I ask for a way to use the color value itself so I could use this code on more complicated examples in future.
[spoiler]
Image

For example changing a certain yellow tone in this image to a special orange or something[/spoiler]
Version: ImageMagick-7.0.7-28-Q16-x64-static http://www.imagemagick.org
Copyright: Copyright (C) 1999-2018 ImageMagick Studio LLC
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Convert gray content of gray/white image to black ?

Post by snibgo »

For that image:

Code: Select all

convert Clipboard01.png -auto-level p.png
Sometimes contrast-stretch works better, or level, or level-colors.

See http://www.imagemagick.org/script/comma ... ptions.php
snibgo's IM pages: im.snibgo.com
Rye
Posts: 158
Joined: 2013-02-25T10:43:05-07:00
Authentication code: 6789

Re: Convert gray content of gray/white image to black ?

Post by Rye »

Is this also possible by using the HexCodes I mentioned
Version: ImageMagick-7.0.7-28-Q16-x64-static http://www.imagemagick.org
Copyright: Copyright (C) 1999-2018 ImageMagick Studio LLC
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Convert gray content of gray/white image to black ?

Post by snibgo »

Code: Select all

convert Clipboard01.png -level-colors #c8c8c8,white p.png
snibgo's IM pages: im.snibgo.com
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Convert gray content of gray/white image to black ?

Post by fmw42 »

Code: Select all

convert Clipboard01.png -fuzz 10% -fill black -opaque "#c8c8c8" result.png
change the fuzz value to whatever works best
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: Convert gray content of gray/white image to black ?

Post by anthony »

That latter (using -opaque) is probably not good as the image is full of shades of gray, and -opqaue is a boolean (all or nothing) type operator.

-level-colors will give ber find control over the transformation. -level will give grayscale control -normalize and -auto-level will automatically maximise contrast (though with different 'level' selection)
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
Rye
Posts: 158
Joined: 2013-02-25T10:43:05-07:00
Authentication code: 6789

Re: Convert gray content of gray/white image to black ?

Post by Rye »

Ok, this works really well. Thanks.

Another question:

If I wanted to change from one hex code to a specific other one.
What woudl I have to change in that code of yours ?
Version: ImageMagick-7.0.7-28-Q16-x64-static http://www.imagemagick.org
Copyright: Copyright (C) 1999-2018 ImageMagick Studio LLC
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Convert gray content of gray/white image to black ?

Post by fmw42 »

Code: Select all

convert image -fuzz XX% -fill "newhexcode" -opaque "oldhexcode" result
Rye
Posts: 158
Joined: 2013-02-25T10:43:05-07:00
Authentication code: 6789

Re: Convert gray content of gray/white image to black ?

Post by Rye »

Thanks ! Just what I was looking for!
Version: ImageMagick-7.0.7-28-Q16-x64-static http://www.imagemagick.org
Copyright: Copyright (C) 1999-2018 ImageMagick Studio LLC
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Convert gray content of gray/white image to black ?

Post by fmw42 »

Post Reply