How to change the color of an transparent png

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
froesi

How to change the color of an transparent png

Post by froesi »

Hi guys,

I've a vector graphic with a single color as an png file. To look smooth the
graphic has an anti aliasement.
I tried something like this

-fuzz 4 % -fill "#FFFFFF" -opaque "#000000", but the thing is, that not
every pixel of the graphic will be conerted to the new color. There is still a halo of the original color around the shape.
I need something like this

http://www.lsus.edu/sc/math/rmabry/imag ... ltireplace
->>"Replace black with blue #0000ff:"

I've no idea how to get this. It would be really great, if someone can give
me a hint or a quick example
Thank you,

cheers
froesi
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: How to change the color of an transparent png

Post by fmw42 »

I don't know if this is the most efficient way, but you could try
separating the image and the alpha channel, modify the image, then
recombine the alpha channel with the modified image.


convert image \
\( -clone 0 +matte -fuzz 4 % -fill "#FFFFFF" -opaque "#000000" \) \
\( -clone 0 -channel matte -separate +matte \) \
-delete 0 +matte -compose copy_opacity -composite result

see http://www.imagemagick.org/Usage/compose/#copyopacity
froesi

Re: How to change the color of an transparent png

Post by froesi »

Thank you for your reply, but the thing isn't workung.

It changes the background to #FFFFFF instead of the picture.

i do the following.

convert -draw 'affine 1,0,0,1,6,77 image Over 0,0 0,0 "background.jpg"' \( -clone 0 +matte -fuzz 5% -fill "#ffffff" -opaque "#000000" \) \( -clone 0 -channel matte -separate +matte \) -delete 0 +matte -compose copy_opacity -composite -draw 'affine 1.4375694885254,0,0,1.4375694885254,242.08215,70.325475 image Over 0,0 0,0 "gra.png"' wallpaper.jpg 2>&1

Do you have another idea what i can do or what is wrong?

thank you guys
cheers
froesi
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: How to change the color of an transparent png

Post by anthony »

the problem is you image is NOT a single color, but a gradient of colors, even if one color is 'transparent'.

If the image has a transparent background, then you can just -colorize the image to set a different color.

If your image is two colors (a gradient between those two colors)
then you can first gray scale and normalize the image to pure black and white. You can then recolor the result using techniques found in..
http://imagemagick.org/Usage/channels/#masks

or do a histogram recoloring using
http://imagemagick.org/Usage/color/#linear
OR a gradient replacement by lookup
http://imagemagick.org/Usage/color/#recolor

You have lots of options that do NOT involve horrible single color replacement, which is the cause of you halo effect!
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
Post Reply