How to make black and white image?

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
Lukas1234

How to make black and white image?

Post by Lukas1234 »

Function monochrome doesnt work since it making it picture look weird.I think proper function will be channel,but i dont know what i need to type exactly.For example in program gimp making black/white picture is called desaturate/lightning
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: How to make black and white image?

Post by fmw42 »

I am not sure what you mean by "black and white"? Two colors only?

If you want grayscale from color, you can type:

convert <input> -colorspace Gray <output>

or

convert <input> -type Grayscale <output>

These will convert color to gray.

However, if you want two colors only (black and white), then you need to threshold, for example, to select the color where above will be white and below will be black

convert <input> -threshold xx% <output>

where xx is in range 0-100 (for percent)
Lukas1234

Re: How to make black and white image?

Post by Lukas1234 »

First command works fine,thank you.
JustForThisPost!
Posts: 1
Joined: 2014-06-18T12:39:50-07:00
Authentication code: 6789

Re: How to make black and white image?

Post by JustForThisPost! »

And so does the second one!

convert <input> -type Grayscale <output>
works as well, thanks!
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: How to make black and white image?

Post by snibgo »

For many other methods, see my page on "Making an image grayscale (monochrome)".
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: How to make black and white image?

Post by fmw42 »

As snibgo said, there are many ways and types of color to grayscale conversion. For others on Linux, Mac OS or Windows with Cygwin, I have a script call color2gray at the link below and the page shows examples, also.

Some concepts involved are:

1) -colorspace and type of intensity-like channel (http://www.imagemagick.org/script/comma ... colorspace)
2)-grayscale and one of its intensity settings (http://www.imagemagick.org/script/comma ... #intensity and http://www.imagemagick.org/script/comma ... #grayscale)
3) r,g,b color channel mixing using -color-matrix (http://www.imagemagick.org/script/comma ... lor-matrix) or -separate with -poly (http://www.imagemagick.org/script/comma ... s.php#poly)
Werty
Posts: 66
Joined: 2010-08-06T05:37:36-07:00
Authentication code: 8675308

Re: How to make black and white image?

Post by Werty »

I like separating to RGB, then make a mean average (-evaluate-sequence mean) of channel R and G, then a mean average of channel G and B, finally a mean average of those two results, RG and GB, gives it more "life" imo.

Here in color, then normal "-type grayscale", then the method mentioned above.
Image

Reason, I guess, is that there is much more green in the world than red, and especially blue, not much blue around if you look, so by adding more green in the grayscale makes it look more natural, if you can call grayscale natural, but again, that just my opinion.
Windows 7 user
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: How to make black and white image?

Post by fmw42 »

Werty wrote:I like separating to RGB, then make a mean average (-evaluate-sequence mean) of channel R and G, then a mean average of channel G and B, finally a mean average of those two results, RG and GB, gives it more "life" imo.
Interesting approach.

If I might suggest in the future that you upload 3 separate images rather than one appended one. That makes it easier for the rest of us to download and do comparisons visually or mathematically. Thanks.
Werty
Posts: 66
Joined: 2010-08-06T05:37:36-07:00
Authentication code: 8675308

Re: How to make black and white image?

Post by Werty »

fmw42 wrote:
Werty wrote: If I might suggest in the future that you upload 3 separate images rather than one appended one. That makes it easier for the rest of us to download and do comparisons visually or mathematically. Thanks.
Sorry about that :?

Here they are in higher resolution...

Color
http://peecee.dk/uploads/062014/color.png

-type Grayscale
http://peecee.dk/uploads/062014/gray.png

grayRGB
http://peecee.dk/uploads/062014/grayRGB.png

So I do...

convert color.png -channel R -separate R.png
convert color.png -channel G -separate G.png
convert color.png -channel B -separate B.png
convert R.png G.png -evaluate-sequence mean RG.png
convert G.png B.png -evaluate-sequence mean GB.png
Convert RG.png GB.png -evaluate-sequence mean grayRGB.png
Windows 7 user
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: How to make black and white image?

Post by fmw42 »

Thanks for separating the images. Here is unix syntax to do it all in one command.

Code: Select all

convert color.png \
\( -clone 0 -channel RG -separate +channel -evaluate-sequence mean \) \
\( -clone 0 -channel GB -separate +channel -evaluate-sequence mean \) \
-delete 0 -evaluate-sequence mean color2gray1.png
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: How to make black and white image?

Post by snibgo »

Another way of doing the same thing, Windows BAT syntax:

Code: Select all

%IM%convert color.png ^
  -separate ^
  -poly "0.25,1, 0.5,1, 0.25,1" ^
  g.png
The 3 weights for the R,G,B channels are 0.25, 0.5 and 0.25. These give the same results as the mean of RG and GB means. You can tweak the three numbers, so long as they add to one. (Of even not, if you want.)
snibgo's IM pages: im.snibgo.com
Werty
Posts: 66
Joined: 2010-08-06T05:37:36-07:00
Authentication code: 8675308

Re: How to make black and white image?

Post by Werty »

Thanks both.

I just sat and starred at the images for 5 mins, and there really is a lot more detail using that method by adding more green, look at the grass and the flowers, more depth and detail.

Also looked at your page snibgo, about grayscale, some interesting results.
Windows 7 user
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: How to make black and white image?

Post by fmw42 »

snibgo wrote:Another way of doing the same thing, Windows BAT syntax:

Code: Select all

%IM%convert color.png ^
  -separate ^
  -poly "0.25,1, 0.5,1, 0.25,1" ^
  g.png
The 3 weights for the R,G,B channels are 0.25, 0.5 and 0.25. These give the same results as the mean of RG and GB means. You can tweak the three numbers, so long as they add to one. (Of even not, if you want.)

Nice simplification, snibgo!

Glad to see that some one else finds my suggested -poly command useful.
Post Reply