How to convert a photo to a black and white image by ImageMagick?

14

1

How can I convert a JPEG photo to black and white (not grayscale) image like output of a FAX scanner, by ImageMagick?

ohho

Posted 2012-03-28T10:33:41.137

Reputation: 2 356

http://unix.stackexchange.com/questions/108613/how-do-you-binarize-a-colored-image – Ciro Santilli 新疆改造中心法轮功六四事件 – 2015-09-25T19:35:15.823

No ImageMagick requirement: http://superuser.com/questions/75373/convert-color-photos-of-documents-to-good-black-and-white-bitonal-images

– Ciro Santilli 新疆改造中心法轮功六四事件 – 2015-09-26T08:21:16.357

Answers

11

According to this forum post:

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).

Der Hochstapler

Posted 2012-03-28T10:33:41.137

Reputation: 77 228

8

Dithering is clearer and more fax-like than a threshold cutoff:

convert <input> -monochrome <output>

For a less contrasty but more information-preserving kind of dithering, use:

convert <input> -remap pattern:gray50 <output>

(Docs)

hemflit

Posted 2012-03-28T10:33:41.137

Reputation: 326

1Note that if the output looks low-quality, you may need to set the density higher using the flag -density 150 (the 150 is a dpi value) – David Fraser – 2015-12-22T15:22:31.473

4

According to this answer here:

If you have imagemagick installed:

true grayscale only:

convert source.jpg -colorspace Gray destination.jpg

true black and white:

convert source.jpg -monochrome destination.jpg

separate into gray channels:

convert source.jpg -separate destination.jpg

user162573

Posted 2012-03-28T10:33:41.137

Reputation:

0

I believe that Netpbm's pamthreshold is a much faster and more flexible solution.

For TIFF files, I do

 $ tifftopnm test.tiff | pamthreshold | pamtotiff > bitonal.tiff

For Jpeg files you can do

$ jpegtopnm test.jpeg | pamthreshold | pamtotiff > bitonal.tiff

Pamthreshold is rather powerful (take a look at its man page).

Maxim

Posted 2012-03-28T10:33:41.137

Reputation: 1 107