Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

My friend has an colored image with Chinese handwriting (basically by taking photo of or scanning what he wrote on a piece of white paper), and he would like me to convert it into a black and white binary image. Are there applications under Ubuntu that can accomplish that?

Here is an example image:

enter image description here

share|improve this question
add comment

2 Answers

up vote 15 down vote accepted

What you want is referred to as "threshold" in image processing. Basically, it takes an image as an input and outputs an image that has all pixels with a value below a given threshold set to black, and all pixels the value of which is above the threshold set to white. This results in a black-and-white image from an arbitrary input image.

Generally, you want to convert to grayscale first for more predictable results, but it is possible to threshold a full-color image as well.

You can use a graphical tool such as GIMP to do this interactively (you'll find the tool through the main menu -> Colors -> Threshold), or you can use ImageMagick something like this:

convert colored.png -threshold 75% thres_colored.png

Running the above command on the example image produces the result shown below.

Black-and-white version of OP's image

Since thresholding is often somewhat of a trial-and-error process to get a result you're happy with, particularly if the source image is not very close to black-and-white already, I recommend the GUI approach if possible, but if that is not an option for whatever reason you can do it through the command line as well. For finer control of the output, you can use tools like color curves, levels and contrast first to isolate the light and dark portions of the image better before thresholding. (Actually, threshold can be seen as an extreme case of using the color curves tool.)

share|improve this answer
1  
Thanks! Since a colored image has RBG three channels, what does/can the threshold apply to in general? –  Tim Jan 9 at 23:00
    
@Tim It probably depends on the software, but I would expect the threshold (unless you specify per channel or for a specific channel, see e.g. ImageMagick's convert's -channel option) to be applied to some sort of "value" of the pixel, which is computed from all channels. That's the reason why I said you might want to convert to grayscale first for more predictable results. (Also see my edit.) –  Michael Kjörling Jan 10 at 8:17
    
Thanks! Does there exist some documentation for what the threshold apply to in the command shown in your post? –  Tim Jan 10 at 18:29
    
@Tim Not really. I expected convert to take either a percentage of the maximum value (which should have been 256 per channel) or a specific value, but I could only get a useful result when specifying a percentage. When you do it with a graphical tool, including GIMP, you'll generally have a histogram that shows the tonal distribution of the image; that will be a great aid in picking the proper value. Using only the command line, unless you have a specific reason to do so, is probably more trouble than it's worth, really. –  Michael Kjörling Jan 10 at 18:31
1  
As a side note, there exist other thresholding methods that don't have to rely on a hard-coded threshold level. For example, ImageMagick includes -lat which performs a local adaptive threshold, taking into account the surrounding pixels. –  voithos Jan 15 at 22:37
add comment

You can use Imagemagick:

convert test.png -colorspace Gray gray_colorspace.png

From here.

Here is what I got after applying to your image:

enter image description here

share|improve this answer
3  
"Binary" comes from "bi" meaning "two", so I assume the OP wants to convert the image to pure black-and-white. Converting to grayscale yields a lot more than two levels. –  Michael Kjörling Jan 9 at 20:57
    
Agreed! makes sense, +1. –  Ketan Jan 9 at 21:31
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.