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

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!

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

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

You should try the level command:
convert test.png -level 25%,75% test_level.png

Related

How to replace one color in a image with another one?

I've this image:
Its background (and only) color is "#687a40" but want to change it for the color "#9ea09a" (a light grey).
I've tried with this command...:
gm convert photo.png -fuzz 0% -fill "#9ea09a" -opaque "#678a40" photoGREY.png
...but I've not seen any effect on the image. I'm sure I'm doing something wrong but GM's documentation is not very friendly.
Your image has an alpha channel, so you need:
gm convert input.png -fill "#9ea09a" -opaque "#687a40ff" result.png

How can I Convert grayscale image to false color gradient with imagemagick

input b/w, output with CLUT
I want to create false color images from grayscale input images. Similar to the typical false color images produced by nasa from b/w inputs to highlight contrast and texture. I can do this in gimp by applying a custom gradient to an indexed color b/w photo. I am sure there is a way to duplicate this with IM but, i cannot find an example in the docs or on stackoverflow. My goal is to automate the process and produce several pseudocolor outputs from each b/w input image. This is for an art project, so no great precision or scientific validity is required. Although, someone may build on my project.
An example would be producing a color relief map from a typical bump map.
Your solution worked. Thank you #MarkSetchell
create new colour look up table named rainbowCLUT.png:
convert -size 1x1! xc:black xc:red xc:orange xc:yellow xc:green xc:blue xc:violet xc:white +append -resize 255x1! rainbowCLUT.png
apply clut to image:
convert FullMoon2010.jpg rainbowCLUT.png -clut result.png;eog result.png
Your solution worked. Thank you #MarkSetchell
create new colour look up table named rainbowCLUT.png:
convert -size 1x1! xc:black xc:red xc:orange xc:yellow xc:green xc:blue xc:violet xc:white +append -resize 255x1! rainbowCLUT.png
apply clut to image:
convert FullMoon2010.jpg rainbowCLUT.png -clut result.png;eog result.png

Resize image to exact size by maintaining aspect ratio nodejs

I want to resize image to an exact size by maintaining aspect ratio and filling the empty space with transparency using nodejs.
I am able to do it using canvas. But due to some server os issues I can not use canvas.
I tried with imageMagick and gm. But couldn't find any option like these. Please show me a way to do this.
Thanks in advance.
In ImageMagick, you can resize and fill to the exact size by
convert image -resize WxH -background none -gravity center -extent WxH output
Input:
Here I will make the background black so you can see that if fills it out.
convert lena.jpg -resize 400x300 -background black -gravity center -extent 400x300 lena1.jpg
In case you have been successful using the canvas for resizing images, you can check out https://github.com/Automattic/node-canvas this repo.
As already mentioned you also resize images using ImageMagick by processes in NodeJS ( http://www.imagemagick.org/Usage/resize/ )
convert dragon.gif -resize 64x64 resize_dragon.gif
In case you have a lot of images, I would suggest that you write a terminal script ( NodeJS can achieve that as well ).
I hope it helps, in case you have more queries, feel free to ask.

Bash: Fill color on specific part of image

I am looking for a way to batch process images in bash.
Is there any way to fill up specific part of image?
Example:
I want to fill up the red part with white color. The red part is always on the top left corner, size is 135x35px.
Yes, Gimp/Photoshop is one way to do this, but I have lot of images and I love automatize things with bash.
Sure, use ImageMagick - installed on most Linux distros and available for OSX and Windows.
convert input.png -fill white -draw "rectangle 0,0 135,35" result.png
If you have many files, make a COPY in a spare folder and try this and it will probably do them all
mogrify -fill white -draw "rectangle 0,0 135,35" *.jpg

ImageMagick - quoted numbers as convert label argument causes the command to fail

my shell hangs on this command:
/usr/local/bin/convert -size 298x71 -background black -fill white -stroke none -gravity center -trim -interline-spacing 0 -font /home/amazingw/public_html/live/fonts/Phillies.ttf label:'150' png:-
The shell just sits there until i press ctrl+c. If I add any letter to the label: argument the command runs fine and I get output.
The font is there but it is missing the number characters in the definition.
Could that cause my command to fail?
This seems like an issue related to input at the console, I am assuming you are using Bash.
If this is the case, Bash is trying to interpret the quotes and is waiting for other input.
after reviewing the imagemagick website for "label:"
Label - Simple Text Label
Basic Labels
Creating a font image using a "label:" image,
is the more typical way of drawing a font
quickly in ImageMagick...
It goes on and gives many examples...
convert -background lightblue -fill blue \
-font Candice -pointsize 72 label:Anthony \
label.gif
None of the examples show single quotes.
I would assume between Bash and ImageMagick's convert program, there is some error on interpretation of input from the console.
I would suggest trying your same command without quotes
/usr/local/bin/convert -size 298x71 -background black -fill white -stroke none -gravity center -trim -interline-spacing 0 -font /home/amazingw/public_html/live/fonts/Phillies.ttf label:150 png:-
I would also take a quick look at the Bash manual about single quotes too.
I was able to test using a modified version of the font which contained the digits. Command went smoothly and an output image was returned. This pretty much narrows it down to an ImageMagick problem with handling labels. Looks like if the label is made out only of characters which are missing in the font (the resulting output image would be empty) - in that case ImageMagick hangs until ctrl+c is pressed - especially problematic as I'm running this from php's proc_open.

Resources