Replace ANY colour Except #ffffff with #000000

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?".
Rye
Posts: 158
Joined: 2013-02-25T10:43:05-07:00
Authentication code: 6789

Replace ANY colour Except #ffffff with #000000

Post by Rye »

So I have this picture:

Image


And want to repace all colours EXCEPT #ffffff with #A7A7AF

I hope this can be done "pixle perfect".

Thanks in advance.
Last edited by Rye on 2014-06-20T12:14:33-07:00, edited 2 times in total.
Version: ImageMagick-7.0.7-28-Q16-x64-static http://www.imagemagick.org
Copyright: Copyright (C) 1999-2018 ImageMagick Studio LLC
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Replace ANY colour Except #ffffff with #000000

Post by fmw42 »

Unfortunately, your image has no pure white #ffffff

If it did, then

Code: Select all

convert "Snow_Sharo 001.gif" -fill "#A7A7AF" +opaque "#ffffff" result.gif
If you want to use a fuzz value to catch the close to white values then try

Code: Select all

convert "Snow_Sharo 001.gif" -fuzz 5% -fill "#A7A7AF" +opaque "#ffffff" result.gif
Adjust the fuzz value as desired
Rye
Posts: 158
Joined: 2013-02-25T10:43:05-07:00
Authentication code: 6789

Re: Replace ANY colour Except #ffffff with #000000

Post by Rye »

the second command seems to do nothing to my picture (I replaced "show:" with result.gif).

Am I doing something wrong ?

I also played around with the fuzz value as instructed, however the change was still - nothing...


This command *did* something, however still not as good as I wished it to be:

Code: Select all

convert "001.gif" -fill white +opaque #A7A7AF -transparent white result.gif
Last edited by Rye on 2014-06-20T12:10:22-07:00, edited 1 time in total.
Version: ImageMagick-7.0.7-28-Q16-x64-static http://www.imagemagick.org
Copyright: Copyright (C) 1999-2018 ImageMagick Studio LLC
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Replace ANY colour Except #ffffff with #000000

Post by fmw42 »

Sorry, I forgot to replace show: with a filename. Edited now to fix that.

If you are on Windows replace % with %%.

Code: Select all

convert "Snow_Sharo 001.gif" -fuzz 5%% -fill "#A7A7AF" +opaque "#ffffff" result.gif

Note the double quotes about your file name, since it has spaces in it. I am not sure how Windows handles that.

see http://www.imagemagick.org/Usage/windows/

Note again, that the image you provide has not perfectly white pixels.
Rye
Posts: 158
Joined: 2013-02-25T10:43:05-07:00
Authentication code: 6789

Re: Replace ANY colour Except #ffffff with #000000

Post by Rye »

using a second % doesn't help.

How can I find the version etc. ?
Version: ImageMagick-7.0.7-28-Q16-x64-static http://www.imagemagick.org
Copyright: Copyright (C) 1999-2018 ImageMagick Studio LLC
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Replace ANY colour Except #ffffff with #000000

Post by fmw42 »

Post your exact command and the input image.

To get the IM version,

Code: Select all

convert -version
Rye
Posts: 158
Joined: 2013-02-25T10:43:05-07:00
Authentication code: 6789

Re: Replace ANY colour Except #ffffff with #000000

Post by Rye »

Code: Select all

convert 001.gif -fuzz 5%% -fill "#A7A7AF" +opaque "#ffffff" 002.gif
This does nothing to the image posted above (have you found a satisfactionary code that works with the above image ?)


IMGMagick version:
Version: ImageMagick 6.8.3-4 2013-02-20 Q16 http://www.imagemagick.org
Copyright: Copyright (C) 1999-2013 ImageMagick Studio LLC
Features: DPC OpenMP
Delegates: bzlib fontconfig freetype jng jp2 jpeg lcms lzma pango png ps tiff x xml zlib
Version: ImageMagick-7.0.7-28-Q16-x64-static http://www.imagemagick.org
Copyright: Copyright (C) 1999-2018 ImageMagick Studio LLC
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Replace ANY colour Except #ffffff with #000000

Post by fmw42 »

Rye wrote:

Code: Select all

convert 001.gif -fuzz 5%% -fill "#A7A7AF" +opaque "#ffffff" 002.gif
This does nothing to the image posted above (have you found a satisfactionary code that works with the above image ?)
It has converted your background from #A8A8B0 to #A7A7AF. But there is little (visual) difference in that color change.

try

Code: Select all

convert 001.gif -fuzz 5%% -fill "#FF0000" +opaque "#ffffff" 002.gif
and you will see that your background is now red
Rye
Posts: 158
Joined: 2013-02-25T10:43:05-07:00
Authentication code: 6789

Re: Replace ANY colour Except #ffffff with #000000

Post by Rye »

Is it possible to define multiple colors that are to be exluded from the replace ?

If I would like to replace any colour except for these three: #F4F4FC #F7F7FF #F9F9FF with let's say #000000.

How'd I do it ?
Version: ImageMagick-7.0.7-28-Q16-x64-static http://www.imagemagick.org
Copyright: Copyright (C) 1999-2018 ImageMagick Studio LLC
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Replace ANY colour Except #ffffff with #000000

Post by fmw42 »

As long as your image has no (partial or fully) transparent pixels, then do the following


Unix:

Code: Select all

convert yourimage \
-fill none -opaque "#F4F4FC" \
-fill none -opaque "#F7F7FF" \
-fill none -opaque "#F9F9FF" \
\( -clone 0 -fill "#000000" -colorize 100  \) \
+swap -compose over -composite resultimage

Windows:

Code: Select all

convert yourimage ^
-fill none -opaque "#F4F4FC" ^
-fill none -opaque "#F7F7FF" ^
-fill none -opaque "#F9F9FF" ^
( -clone 0 -fill  "#000000" -colorize 100 ) ^
+swap -compose over -composite resultimage
Add -fuzz if so desired to get close colors.
Rye
Posts: 158
Joined: 2013-02-25T10:43:05-07:00
Authentication code: 6789

Re: Replace ANY colour Except #ffffff with #000000

Post by Rye »

This outputs:
"convert.exe: invalid argument for option '-colorize': #000000 @ error/convert.c/ConvertImageCommand/969."
Version: ImageMagick-7.0.7-28-Q16-x64-static http://www.imagemagick.org
Copyright: Copyright (C) 1999-2018 ImageMagick Studio LLC
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Replace ANY colour Except #ffffff with #000000

Post by snibgo »

I think Fred made a typo and meant "-colorize 100" (no quotes needed).

The following Windows BAT script makes a sample file. From this, it makes all colours except the specified colours into purple. Change "purple" to anything you want.

There may be a simpler method.

Code: Select all

convert ^
  -size 20x20 ^
  xc:red xc:lime xc:blue xc:#F4F4FC xc:#F7F7FF xc:#F9F9FF xc:khaki ^
  +append ^
  sample.png

convert ^
  sample.png ^
  ( -clone 0 -fill Purple -colorize 100 ) ^
  +swap ^
  -alpha set ^
  ( -clone 1 ^
    ( -clone 0 +transparent #F4F4FC ) ^
    ( -clone 0 +transparent #F7F7FF ) ^
    ( -clone 0 +transparent #F9F9FF ) ^
    -delete 0 ^
    -evaluate set 0 ^
    -layers flatten ^
    -negate ^
  ) ^
  -compose Over -composite ^
  s1.png

convert ^
  s1.png -unique-colors txt:
The final "-unique-colors" is to verify the result.
snibgo's IM pages: im.snibgo.com
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Replace ANY colour Except #ffffff with #000000

Post by fmw42 »

I think Fred made a typo and meant "-colorize 100" (no quotes needed).
Yes, indeed I made a mistake. Thanks for catching it snibgo. I have fixed my command above.
Rye
Posts: 158
Joined: 2013-02-25T10:43:05-07:00
Authentication code: 6789

Re: Replace ANY colour Except #ffffff with #000000

Post by Rye »

Ok, perfect, now it works :)

BTW: colorize seems to only "tone" the color down.

Is there a way to make it pitch black (for example) just by once executing this ?
Version: ImageMagick-7.0.7-28-Q16-x64-static http://www.imagemagick.org
Copyright: Copyright (C) 1999-2018 ImageMagick Studio LLC
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Replace ANY colour Except #ffffff with #000000

Post by fmw42 »

use

Code: Select all

-fill black -colorize 100
The 100 means totally change to that exact color. Alternately, you can use

Code: Select all

-evaluate set 0
In place of the above. That will make any image totally black. There are lots of ways to do this.

see
http://www.imagemagick.org/Usage/canvas/#solid
and especially
http://www.imagemagick.org/Usage/canvas/#other
Post Reply