How to remove or make transparent all but one single color.

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
CannedCheese

How to remove or make transparent all but one single color.

Post by CannedCheese »

I'm attempting to save an alphabet of bmps (or any format) of some all gray text characters and numbers that I have to screen capture in front of various backgrounds. The characters are always the exact same shade of Gray (0xC0C0C0C). Is there a way to remove or make transparent all colors that don't match this shade of gray? This is for optical character recognition purposes.

Cliff Notes: I want pure gray solid characters and to remove all background colors.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: How to remove or make transparent all but one single color.

Post by fmw42 »

Bonzo
Posts: 2971
Joined: 2006-05-20T08:08:19-07:00
Location: Cambridge, England

Re: How to remove or make transparent all but one single color.

Post by Bonzo »

Try this:

Code: Select all

convert original_image.jpg -matte ( +clone -fuzz 20% -transparent rgb(38,134,71) ) -compose DstOut -composite output.png
CannedCheese

Re: How to remove or make transparent all but one single color.

Post by CannedCheese »

I appreciate the response. I'm unaware of where to download a compiled version of the beta (I no longer have the compiler to compile the beta on my own). I've searched the faq and googled, etc-- is there a location to download a compiled version of the betas that runs on XP?
CannedCheese

Re: How to remove or make transparent all but one single color.

Post by CannedCheese »

Bonzo wrote:Try this:

Code: Select all

convert original_image.jpg -matte ( +clone -fuzz 20% -transparent rgb(38,134,71) ) -compose DstOut -composite output.png
Thanks, I will try this tonight as well.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: How to remove or make transparent all but one single color.

Post by fmw42 »

IM 6.4.1-4 is still not out yet. But you can get binary versions from http://www.imagemagick.org/script/binary-releases.php and source from http://www.imagemagick.org/download/www ... ource.html
CannedCheese

Re: How to remove or make transparent all but one single color.

Post by CannedCheese »

fmw42 wrote:IM 6.4.1-4 is still not out yet. But you can get binary versions from http://www.imagemagick.org/script/binary-releases.php and source from http://www.imagemagick.org/download/www ... ource.html
So there's no compiled beta to be downloaded from ftp://ftp.fifi.org/pub/ImageMagick/beta ?
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: How to remove or make transparent all but one single color.

Post by fmw42 »

Not that I know of - only source beta that you need to compile.
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: How to remove or make transparent all but one single color.

Post by anthony »

Use +opaque which inverts the meaning of the color selection. That is replace pixels which are NOT this color (or near this color according to the fuzz factor.

This was added to IM v6.3.7-10 see Replace a Specific Color
Those examples also gives the same solution as above for IM versions pre-dating this addition.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
dovo329
Posts: 1
Joined: 2015-06-13T15:51:13-07:00
Authentication code: 6789

in 2015 imagemagick on mac above didn't work but this did:

Post by dovo329 »

#To make all other colors in picture except red transparent:

convert input.png -transparent 'rgb(255,0,0)' matteInput.png
convert input.png -matte matteInput.png -compose DstOut -composite output.png
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: How to remove or make transparent all but one single color.

Post by fmw42 »

try +transparent

Code: Select all

convert input.png +transparent red result.png
This will make everything but pure red transparent
Post Reply