Trying to remove all colors except black

Questions and postings pertaining to the usage of ImageMagick regardless of the interface. This includes the command-line utilities, as well as the C and C++ APIs. Usage questions are like "How do I use ImageMagick to create drop shadows?".
Post Reply
jswolcott
Posts: 8
Joined: 2015-02-07T17:42:43-07:00
Authentication code: 6789

Trying to remove all colors except black

Post by jswolcott »

convert C:\temp\color.tiff -fuzz 10% -fill white -opaque black c:\temp\bw.tiff

The output file is 100% identical to the input.

I'm clearly missing some thing very simple but I haven't the foggiest.
jswolcott
Posts: 8
Joined: 2015-02-07T17:42:43-07:00
Authentication code: 6789

Re: Trying to remove all colors except black

Post by jswolcott »

Wow. I can not say how much of a dope I feel right now.

Thank you for pointing that out though.

wow
jswolcott
Posts: 8
Joined: 2015-02-07T17:42:43-07:00
Authentication code: 6789

Re: Trying to remove all colors except black

Post by jswolcott »

Alright, well I do also have imagemagick. I ran this by imagemagick (not graphics magick this time) and replaced the -opaque with a +opaque.

Now I do get changed output. However I'm getting a plain white wash. No black or any thing like it. I also tried the double %% as suggested. Not a windows expert either but we use what we are given eh?
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Trying to remove all colors except black

Post by fmw42 »

Upload your tiff image to dropbox.com and put the URL here. We need to see your input so we can test with it. Does it have any alpha transparency?

If you are getting totally white output, perhaps you fuzz value is too large. Try smaller values

What version of Imagemagick are you using? Perhaps it is too old or buggy.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Trying to remove all colors except black

Post by snibgo »

Code: Select all

convert C:\temp\color.tiff -fuzz 10% -fill white -opaque black c:\temp\bw.tiff
You seem to be running this from the command line. Don't double the percent sign. The rule about doubling them applies only to BAT files (scripts).
snibgo's IM pages: im.snibgo.com
jswolcott
Posts: 8
Joined: 2015-02-07T17:42:43-07:00
Authentication code: 6789

Re: Trying to remove all colors except black

Post by jswolcott »

The link is on drive here:
https://drive.google.com/file/d/0B2aTfI ... sp=sharing

should be public


Oh.. Well here is relevant info. It's also outputting this error:
convert.exe: Unknown field with tag 20624 (0x5090) encountered. `TIFFReadDirectory' @ warning/tiff.c/TIFFWarnings/857.
convert.exe: Unknown field with tag 20625 (0x5091) encountered. `TIFFReadDirectory' @ warning/tiff.c/TIFFWarnings/857.

All I'm trying to do is get all the colors out of this image so that all is left is black, which is primarily text, so I can OCR it.
jswolcott
Posts: 8
Joined: 2015-02-07T17:42:43-07:00
Authentication code: 6789

Re: Trying to remove all colors except black

Post by jswolcott »

version mageMagick-6.9.0-Q16 windows.
User avatar
glennrp
Posts: 1147
Joined: 2006-04-01T08:16:32-07:00
Location: Maryland 39.26.30N 76.16.01W

Re: Trying to remove all colors except black

Post by glennrp »

Try the -threshold option then, e.g., -threshold 30%. Anything darker than 30% intensity becomes black and the rest become white.

e.g.,

Code: Select all

gm convert logo: -colorspace gray -threshold 30% gmbw.png
or

Code: Select all

convert logo: -colorspace gray -threshold 30% imbw.png
jswolcott
Posts: 8
Joined: 2015-02-07T17:42:43-07:00
Authentication code: 6789

Re: Trying to remove all colors except black

Post by jswolcott »

Well that kind of works. However it keeps a ton of the images which are a dark color but not black... I may be barking up the wrong tree here. I'm just trying to get this simple for tesseract and find a way to automate the process.... cropping is so error prone since the images I'm taking in are not always uniform. -sigh-
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Trying to remove all colors except black

Post by fmw42 »

try this

Code: Select all

convert color3.tiff -alpha off -fuzz 35% -fill white +opaque black result.tiff
Adjust the fuzz value as desired. Note you have no pure black in the image. That is why when you use a small fuzz value it shows as pure white.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Trying to remove all colors except black

Post by snibgo »

"-contrast-stretch" can be useful, eg increase the contrast until 30% of pixels are black and 20% are white:

Code: Select all

convert color3.tiff -contrast-stretch 30,20% t.png
snibgo's IM pages: im.snibgo.com
jswolcott
Posts: 8
Joined: 2015-02-07T17:42:43-07:00
Authentication code: 6789

Re: Trying to remove all colors except black

Post by jswolcott »

Wanted to swing by and say thanks to ya'll. I ended up using
convert color3.tiff -contrast-stretch 35,25% t.png
followed by
convert color3.tiff -alpha off -fuzz 10% -fill white +opaque black result.tiff

Worked perfectly. Removed the color backgrounds entirely and I was then able to crop and OCR the text boxes. Thanks!
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Trying to remove all colors except black

Post by snibgo »

Your first command "convert color3.tiff -contrast-stretch 35,25% t.png" creates t.png. If you don't use t.png, it is pointless.
snibgo's IM pages: im.snibgo.com
jswolcott
Posts: 8
Joined: 2015-02-07T17:42:43-07:00
Authentication code: 6789

Re: Trying to remove all colors except black

Post by jswolcott »

well yeah modified for correct file names. It's inside a script and runs on many images so.. any way. point is it worked.
Post Reply