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.
Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.


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.
Many programs have tried to outwit PowerPoint in terms of technology and skill. These programs, however, still lack several characteristics that PowerPoint has possessed from the start. Here's why PowerPoint replacements won't entirely work for desi…
Learn how to use the remote presentation tool in Prezi to allow you to harness the power of this cloud based presentation platform. You can show your presentation with a remote audience using this free tool.
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

816 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

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now