Solved

ImageMagick: Greyscale to black and white conversion

Posted on 2004-04-23
7
5,171 Views
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.
0
Comment
Question by:phasevar
  • 3
  • 3
7 Comments
 

Expert Comment

by:krisnackaerts
Comment Utility
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
0
 

Author Comment

by:phasevar
Comment Utility
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.  
0
 

Expert Comment

by:krisnackaerts
Comment Utility
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.
0
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 

Author Comment

by:phasevar
Comment Utility
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.
0
 

Accepted Solution

by:
krisnackaerts earned 500 total points
Comment Utility
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?
0
 

Author Comment

by:phasevar
Comment Utility
Linux.

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

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

I've been using this technique since Adobe CS2, and it should work with any version of Illustrator that includes the appearance panel. In this tutorial we'll create a button using the appearance panel in Adobe Illustrator, and then save it as a r…
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…
In this Micro Tutorial viewers will learn the basic shortcuts and functions of Illustrator. The viewer will learn about the paintbrush tool, anchor points, font sizing, and more.
Learn how to create interesting presentations by including videos to keep your audience engaged using Prezi. Select "Insert" from the top menu in your Prezi editor: Select "YouTube Video": Paste the video URL into the prompt: "Select "Insert":…

772 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

12 Experts available now in Live!

Get 1:1 Help Now