swapping a colour while maintaining alpha channel

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
sam_anthony
Posts: 2
Joined: 2011-07-28T06:49:47-07:00
Authentication code: 8675308

swapping a colour while maintaining alpha channel

Post by sam_anthony »

Source image: http://www.realtyhub.com.au/images/icon ... 000000.png
This image only has one colour, black.
The anti aliasing is done with pure black pixles which are semitransparent.
I want to swap the black with another colour, however when I do the semi transparent black is not changed.
Using command:

convert icons.png -fill red -opaque black out.png

Output image: http://www.realtyhub.com.au/images/out.png

Need to use semi transparent anti aliasing as the image will be ontop of a item of unknown colour.
Thanks

ImageMagick 6.7.1-0 – Windows 7
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: swapping a colour while maintaining alpha channel

Post by fmw42 »

try this


convert icons_000000.png -alpha off -fill red -opaque black -alpha on icons_000000_red.png
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: swapping a colour while maintaining alpha channel

Post by anthony »

As a FYI, what that suggestion does is 'disable alpha channel' so it is not seen, modify the image, then re-enable the preserved alpha.

Other techniques include using +level-colors dodgerblue with the default channel setting (which does not include alpha). Or one of many other Global Color Modification of the image.

Another suggestion is that as the image is a 'shape mask', then you can just extract the alpha, and then use it as a colored shape mask.

Code: Select all

 convert icon.png  -alpha extract  -background seagreen -alpha shape result.png
See http://www.imagemagick.org/Usage/masking/#shapes
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
sam_anthony
Posts: 2
Joined: 2011-07-28T06:49:47-07:00
Authentication code: 8675308

Re: swapping a colour while maintaining alpha channel

Post by sam_anthony »

Thanks for your help, what you suggested worked.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: swapping a colour while maintaining alpha channel

Post by fmw42 »

As Anthony points out, the reason your method did not work was all colors in a transparent image need to be specified with alpha values. So red must be, for example, rgb(255,0,0,1), and black must be rgb(0,0,0,1) where both are opaque. So rather than mess with this, as I was not sure what the result would be when not modifying partially transparent values, I chose simply to turn off the alpha channel so that one could avoid having to specify the alpha value for each color. Then turn the original alpha channel back on.

Sorry, at the time I wrote my earlier message, I was in a hurry to leave for an appointment, and thought I could get back to the explanation earlier.
Post Reply