How can I convert grayscale image to black&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
macias
Posts: 28
Joined: 2008-12-10T13:44:19-07:00

How can I convert grayscale image to black&white?

Post by macias »

Hello,

In theory -depth 1 or -colors 2 should work, but after converting the image (png file) I check it with Gimp and it says, it is grayscale, not indexed mode. So -- how can I convert it to black&white? Using IM of course, not Gimp :-).

Cheers,
Last edited by macias on 2010-10-29T10:02:02-07:00, edited 1 time in total.
indiego
Posts: 75
Joined: 2010-10-16T06:35:10-07:00
Authentication code: 8675308

Re: How can I conver grayscale image to black&white?

Post by indiego »

macias wrote:Hello,

In theory -depth 1 or -colors 2 should work, but after converting the image (png file) I check it with Gimp and it says, it is grayscale, not indexed mode. So -- how can I convert it to black&white? Using IM of course, not Gimp :-).

Cheers,
try

-type GrayScale -depth 1

Works for my png graphic.

Greetings, Peter
macias
Posts: 28
Joined: 2008-12-10T13:44:19-07:00

Re: How can I convert grayscale image to black&white?

Post by macias »

Well, it works in sense, that there is an output, but the output is in grayscale, not indexed black&white. I checked it using Gimp.

Just to be sure, I updated ImageMagick to the latest of the latest -- still grayscale (but this time filesize is a bit smaller). But still, the image is not indexed.
indiego
Posts: 75
Joined: 2010-10-16T06:35:10-07:00
Authentication code: 8675308

Re: How can I convert grayscale image to black&white?

Post by indiego »

You are right. How about

-type Palette -depth 1 -define png:color-type=0
indiego
Posts: 75
Joined: 2010-10-16T06:35:10-07:00
Authentication code: 8675308

Re: How can I convert grayscale image to black&white?

Post by indiego »

Sorry, again grayscale (wrong color-type number). This should work (hopefully) :D

-type Palette -define png:color-type=3 -define png:bit-depths=1

BTW: there is a typo in

http://www.imagemagick.org/Usage/formats/#png_write

bit-depth instead of bit-depths
indiego
Posts: 75
Joined: 2010-10-16T06:35:10-07:00
Authentication code: 8675308

Re: How can I convert grayscale image to black&white?

Post by indiego »

This is really not easy. I tested some more and got an indexed 2 colored image with

-type bilevel -define png:color-type=3

This time tested with gimp. Sorry for all the wrong solutions, but I'm a IM beginner. :?
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: How can I convert grayscale image to black&white?

Post by fmw42 »

Many of IM's PNG features are not currently working correctly. See viewtopic.php?f=3&t=16490
macias
Posts: 28
Joined: 2008-12-10T13:44:19-07:00

Re: How can I convert grayscale image to black&white?

Post by macias »

Indiego, thanks. Now I get the indexed image.

Strange thing though, it is as big as grayscale image (it should be smaller, because now each pixel takes only 1 bit) -- for comparison Gimp is way more efficient. I will look further into those options.

Thanks again!

PS. I used this link:
http://www.imagemagick.org/Usage/formats/#png_write

And testes such options:
-type bilevel -define png:color-type=3 -define png:bit-depth=1

In theory it should work, but instead I get error:
convert: Cannot write image with defined PNG:bit-depth or PNG:color-type.

IM in regard to PNG is broken now indeed.
Post Reply