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

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?".
artech
Posts: 10
Joined: 2018-10-08T09:10:04-07:00
Authentication code: 1152

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

Post 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
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

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

Post 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
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: changing the whole color of a png icon preserving the edges alpha

Post 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
artech
Posts: 10
Joined: 2018-10-08T09:10:04-07:00
Authentication code: 1152

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

Post 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
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

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

Post by snibgo »

What version of IM? On what platform?
snibgo's IM pages: im.snibgo.com
artech
Posts: 10
Joined: 2018-10-08T09:10:04-07:00
Authentication code: 1152

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

Post by artech »

ImageMagick-7.0.8-11-Q16-x64 on windows 10
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

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

Post by snibgo »

Why are you using "convert"? For v7, I suggest you use "magick". Not "convert", and not "magick convert".
snibgo's IM pages: im.snibgo.com
artech
Posts: 10
Joined: 2018-10-08T09:10:04-07:00
Authentication code: 1152

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

Post by artech »

can you give me a suggestion how to do such a thing with magick?
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

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

Post 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
artech
Posts: 10
Joined: 2018-10-08T09:10:04-07:00
Authentication code: 1152

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

Post by artech »

Again the result was the same. It fills the whole of image to that color like following:

Image
User avatar
GeeMack
Posts: 718
Joined: 2015-12-01T22:09:46-07:00
Authentication code: 1151
Location: Central Illinois, USA

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

Post 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
artech
Posts: 10
Joined: 2018-10-08T09:10:04-07:00
Authentication code: 1152

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

Post by artech »

@GeeMack, it was the magical command ;) you saved me. Thank you all for your helps
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

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

Post 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.
artech
Posts: 10
Joined: 2018-10-08T09:10:04-07:00
Authentication code: 1152

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

Post by artech »

I have another question. In above command how can I fill with gradient instead of solid color and again with preserving alpha?
User avatar
GeeMack
Posts: 718
Joined: 2015-12-01T22:09:46-07:00
Authentication code: 1151
Location: Central Illinois, USA

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

Post 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 "%%".
Last edited by GeeMack on 2018-10-09T09:40:41-07:00, edited 1 time in total.
Post Reply