[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now


ImageMagick: Greyscale to black and white conversion

Posted on 2004-04-23
Medium Priority
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
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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.
Enroll in October's Free Course of the Month

Do you work with and analyze data? Enroll in October's Course of the Month for 7+ hours of SQL training, allowing you to quickly and efficiently store or retrieve data. It's free for Premium Members, Team Accounts, and Qualified Experts!


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 1000 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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

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

Question has a verified solution.

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

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…
Technology opened people to different means of presenting information, but PowerPoint remains to be above competition. Know why PPT still works today.
HTML5 has deprecated a few of the older ways of showing media as well as offering up a new way to create games and animations. Audio, video, and canvas are just a few of the adjustments made between XHTML and HTML5. As we learned in our last micr…
It’s easy to embed any of your public Prezi presentations on your website or social network to share with others. Learn how simple it is in this tutorial.
Suggested Courses

649 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