Page 1 of 2

changing the whole color of a png icon preserving the edges alpha

Posted: 2018-10-08T09:33:48-07:00
by artech
Hi friends

I did lots of search in forums but couldn't solve this. I have an black icon with alpha edges. I want to change the whole color of the icon without losing the alpha. I use the follwing command:

Code: Select all

convert icon.png -fuzz 50% -fill "#550000" -opaque "#000000" icon2.png
It's my source image:
Image

It's my output:
Image

It changes the color but alpha will be loosed.
I do such a thing in for example gimp with locking the alpha channel but here I can't find how to do it.
Thanks in advance

Re: changing the whole color of a png icon preserving the edges alpha

Posted: 2018-10-08T09:48:29-07:00
by snibgo
Your input is entirely black, with varying alpha. We can change it to be any solid colour we want eg blue, keeping the alpha:

Code: Select all

convert icon.png -fill blue -colorize 100 out.png

Re: changing the whole color of a png icon preserving the edges alpha

Posted: 2018-10-08T09:50:28-07:00
by fmw42
Turn alpha off, do your processing, then turn alpha on.

Code: Select all

convert icon.png -alpha off -fuzz 50% -fill "#550000" -opaque "#000000" -alpha on icon2.png
Does that work for you. It is fine for me on IM 6.9.10.12 Q16 Mac OSX Sierra

Re: changing the whole color of a png icon preserving the edges alpha

Posted: 2018-10-08T10:01:26-07:00
by artech
@snibgo , thanks, it works great for this icon.

another question is that what if i have a for example white spot on the icon and want to preserve that?

@fmw42 , I tested with your suggestion but the output is a colored square. I mean whole the image will turn to that color

Re: changing the whole color of a png icon preserving the edges alpha

Posted: 2018-10-08T10:16:59-07:00
by snibgo
What version of IM? On what platform?

Re: changing the whole color of a png icon preserving the edges alpha

Posted: 2018-10-08T10:25:29-07:00
by artech
ImageMagick-7.0.8-11-Q16-x64 on windows 10

Re: changing the whole color of a png icon preserving the edges alpha

Posted: 2018-10-08T10:27:49-07:00
by snibgo
Why are you using "convert"? For v7, I suggest you use "magick". Not "convert", and not "magick convert".

Re: changing the whole color of a png icon preserving the edges alpha

Posted: 2018-10-08T10:35:26-07:00
by artech
can you give me a suggestion how to do such a thing with magick?

Re: changing the whole color of a png icon preserving the edges alpha

Posted: 2018-10-08T10:54:59-07:00
by fmw42

Code: Select all

magick icon.png -alpha off -fuzz 50% -fill "#550000" -opaque "#000000" -alpha on icon2.png
This should work for Unix and Windows. In a Windows .bat file, double the % to %%.

See
https://imagemagick.org/Usage/windows/
https://imagemagick.org/script/porting.php#cli

Re: changing the whole color of a png icon preserving the edges alpha

Posted: 2018-10-08T11:32:33-07:00
by artech
Again the result was the same. It fills the whole of image to that color like following:

Image

Re: changing the whole color of a png icon preserving the edges alpha

Posted: 2018-10-08T12:14:46-07:00
by GeeMack
artech wrote: 2018-10-08T11:32:33-07:00Again the result was the same. It fills the whole of image to that color like following:
Try the same thing only instead of "-alpha off" just work it with the RGB channels like this...

Code: Select all

magick icon.png -channel RGB -fuzz 50% -fill "#550000" -opaque "#000000" -alpha on icon2.png

Re: changing the whole color of a png icon preserving the edges alpha

Posted: 2018-10-08T12:46:50-07:00
by artech
@GeeMack, it was the magical command ;) you saved me. Thank you all for your helps

Re: changing the whole color of a png icon preserving the edges alpha

Posted: 2018-10-08T16:20:09-07:00
by fmw42
Very strange. The -alpha off and -alpha on work fine for me on IM 6.9.10.12 Q16 Mac OSX. But you are correct that it fails in IM 7. I will report as a bug.

Re: changing the whole color of a png icon preserving the edges alpha

Posted: 2018-10-09T06:30:54-07:00
by artech
I have another question. In above command how can I fill with gradient instead of solid color and again with preserving alpha?

Re: changing the whole color of a png icon preserving the edges alpha

Posted: 2018-10-09T09:26:10-07:00
by GeeMack
artech wrote: 2018-10-09T06:30:54-07:00I have another question. In above command how can I fill with gradient instead of solid color and again with preserving alpha?
You can use "-sparse-color" to add a gradient fill to the red, green, and blue channels of the input image while keeping the transparency of the alpha channel. A simple example command would look like this...

Code: Select all

magick icon.png -channel RGB -sparse-color barycentric "0,0 black %[w],0 white" icon3.png
You can specify any offsets you like in the "-sparse-color" operation by using FX expressions. You can also use any colors. The command above starts the gradient with black at the upper left corner and ends with white at the upper right corner. To make the gradient go from red to green and from the upper left to the lower right, you could use something like this...

Code: Select all

magick icon.png -channel RGB -sparse-color barycentric "0,0 red %[w],%[h] green" icon4.png
This method would also work for making the image a single color by specifying the same color for the start and end of the "-sparse-color" operation, and it can take even more arguments to start several colors at various places in the image. See some instructions for "-sparse-color" at THIS link.

Make sure to set the channels with "-channel RGB" before the coloring operation to make sure it's not modifying the alpha channel. And if necessary for more operations, you can re-activate the alpha channel with "+channel" right after coloring.

As always, in a BAT script you need to double the percent signs "%%".