ImageMagick: Greyscale to black and white conversion

Posted on 2004-04-23
Last Modified: 2012-08-13
I have the latest ImageMagick installed on my server and I'm trying to figure out how I can take an extracted matte image (grayscale) and convert it to black and white in such a way that all pixels with a value between 0 and 254 are converted to 0 and all pixels with a value of 255 are left white.  In other words, for each pixel that is any level of grey, turn it black, any pixels that are pure white, leave them be.
Question by:phasevar
  • 3
  • 3

Expert Comment

ID: 10926380
Start changing the image to GRAY colorspace, to be sure you have an 8bit image:

convert -colorspace GRAY in.png out.png

then threshold the image to the value you want by

convert -threshold 254 out.png bw.png

This should do the trick

Author Comment

ID: 10935329
The first convert produces a grayscale image for me, but the second command's output is a 100% white image and it doesn't matter what I set the threshold to, it turns out as just a plain white image every time.

I tried:

convert -black-threshold 254 out.png bw.png

but that didn't work either, its output looks the same as the input.

I'm using ImageMagick 6.0.0.  

Expert Comment

ID: 10935690
I used 5.5.7. It worked. I uninstallled it now, installed 6.0.0 Q16, tried it, didn't work. So it seems a bug in this version. I then double checked it by uninstalling v6.0.0 again, downloading v5.5.7 Q8, and it does work with this version.

My tip: uninstall 6.0.0 and install 5.5.7 for the moment. Try to log the problem. I logged something similar a while ago and the bug was solved in a few days time. You could also try the latest beta to see if they solved it already.
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.


Author Comment

ID: 10956568
I tried both the latest beta, and the 5.5.7 and both of them produced blank (white) images for me.  

I start with what is clearly a grayscale image when loaded in my browser.  Then after the command

convert -threshold 254 out.png bw.png

bw.png is pure white.

Accepted Solution

krisnackaerts earned 500 total points
ID: 10956586
Next question, which operating system are you using? Windows 95/98/2000/NT/XP, Linux, ...?

It's not because an image shows grayscale, it is grayscale (i.e. 8 bit grayscale). That's why I added the comment above. Also, maybe your white is not exactly white (254), have you tried with v 5.5.7 and other values (e.g. 128 to start with)?

I ask this because it works for me.

Does it need to be imagemagick?

Author Comment

ID: 10956638

Yes, I tried 5.5.7 with a value of 128 and same result.  Doesn't seem to matter what value I use for threshold, the result is always 100% white.

It doesn't have to be ImageMagick I suppose..  I'm not aware of any other command line utilities for this sort of thing.  My app is in Python and unfortunately I can't find all the tools I need there either.

I don't see why it should matter, but the first step in my process is to pull the matte out of a png image like so:

convert light.png -channel matte light_matte.png

Then I convert with -colorspace GRAY, then onto the convert -threshold.

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Keep your audience engaged and get the most out of your next presentation with these quick Prezi tips.
Technology opened people to different means of presenting information, but PowerPoint remains to be above competition. Know why PPT still works today.
Learn how to set up basic frames and paths in Prezi and understand the open space that Prezi allows you to create presentations in.
Learn the basics of inputting and editing your text components in Prezi. We will cover how to set styles, position, and group your text components. In your Prezi editor, click anywhere on the canvas to add text: A flashing cursor informs you that yo…

821 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question