Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using ImageMagick to remove all color except black in an image?

The situation is : I have many images of documents from scanning. I want to keep the document's main content - which is printed in color black (a small range of colors around #000000). But, you know, the documents are always full of colors : stamp, background, decorations, logos...etc.

I just want to keep the TEXTS ONLY which were printed in the color black.

I've tried with ImageMagick and this command so far:

convert X.png -matte (+clone -fuzz 20% -transparent "#000000") -compose DstOut -composite X1.png

But the result was not as expected , the text was very damaged that I cannot read. Someone suggested me to increase the fuzz to 70%:

convert X.png -matte (+clone -fuzz 70% -transparent "#000000") -compose DstOut -composite X1.png

Then the text appeared to be more readable, but the other colors kept remaining too.

Can you please show me a better solution for my situation? Every suggestion would be highly appreciated!

like image 633
vantrung -cuncon Avatar asked Mar 15 '13 07:03

vantrung -cuncon


2 Answers

To match all colors except black you can use +opaque "#000000".

In order to include a little range around #000000 you can try different percentages with the fuzz operator:

convert input.png -fill white -fuzz 10% +opaque "#000000" result.png

Tested with ImageMagick 6.6.0-1 on Windows

like image 186
Andrea Avatar answered Nov 08 '22 08:11

Andrea


I stumbled upon this question while looking for an answer to this question. I found a couple imagemagick forum posts that helped. Here is what I came up with:

convert input.gif -matte \( +clone -fuzz 1 -transparent black \) -compose DstOut -composite ~/Desktop/output.png

Forum post: ImageMagick: Removing all but one color

like image 44
polynomial Avatar answered Nov 08 '22 08:11

polynomial