Page 1 of 2
Replace ANY colour Except #ffffff with #000000
Posted: 2014-06-20T09:55:12-07:00
by Rye
So I have this picture:
And want to repace all colours EXCEPT #ffffff with #A7A7AF
I hope this can be done "pixle perfect".
Thanks in advance.
Re: Replace ANY colour Except #ffffff with #000000
Posted: 2014-06-20T10:09:54-07:00
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
Re: Replace ANY colour Except #ffffff with #000000
Posted: 2014-06-20T12:03:16-07:00
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
Re: Replace ANY colour Except #ffffff with #000000
Posted: 2014-06-20T12:08:49-07:00
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.
Re: Replace ANY colour Except #ffffff with #000000
Posted: 2014-06-20T13:04:49-07:00
by Rye
using a second % doesn't help.
How can I find the version etc. ?
Re: Replace ANY colour Except #ffffff with #000000
Posted: 2014-06-20T14:13:31-07:00
by fmw42
Post your exact command and the input image.
To get the IM version,
Re: Replace ANY colour Except #ffffff with #000000
Posted: 2014-06-20T14:38:36-07:00
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
Re: Replace ANY colour Except #ffffff with #000000
Posted: 2014-06-20T15:00:17-07:00
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
Re: Replace ANY colour Except #ffffff with #000000
Posted: 2014-06-20T15:16:19-07:00
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 ?
Re: Replace ANY colour Except #ffffff with #000000
Posted: 2014-06-20T15:58:50-07:00
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.
Re: Replace ANY colour Except #ffffff with #000000
Posted: 2014-06-21T01:40:10-07:00
by Rye
This outputs:
"convert.exe: invalid argument for option '-colorize': #000000 @ error/convert.c/ConvertImageCommand/969."
Re: Replace ANY colour Except #ffffff with #000000
Posted: 2014-06-21T07:33:36-07:00
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.
Re: Replace ANY colour Except #ffffff with #000000
Posted: 2014-06-21T09:59:35-07:00
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.
Re: Replace ANY colour Except #ffffff with #000000
Posted: 2014-06-21T11:18:58-07:00
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 ?
Re: Replace ANY colour Except #ffffff with #000000
Posted: 2014-06-21T11:58:21-07:00
by fmw42
use
The 100 means totally change to that exact color. Alternately, you can use
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