Converting 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
emafuma
Posts: 9
Joined: 2011-07-15T05:45:37-07:00
Authentication code: 8675308

Converting transparent PNG

Post by emafuma »

Hi, I'm trying to convert a PNG with transparency, but if I add vignette I have my transparency replaced with black.
Is it possible to replace with white
I tried:

convert agrate_brianza.png -background transparent -transparent-color white -gravity Center -crop 150.0x150.0+0+0 -resize '136.0' -opaque white -fill white -transparent white -vignette 1x1+2+2 agg3.png

But didn't work.
Thanks in advance
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Converting transparent PNG

Post by fmw42 »

post a link to your image. also explain what you want to happen with the transparency. it is hard to know what you are trying to do without seeing your image and your bad result and know what you want for a good result. also let us know what version of IM and platform you are using.

you certainly have too many combinations of -fill, -opaque, -transparent (and often in the wrong order) and -transparent color will do nothing.

If I understand, then try

convert image -crop ... -resize ... -alpha off -vignette ... -alpha on resultimage
emafuma
Posts: 9
Joined: 2011-07-15T05:45:37-07:00
Authentication code: 8675308

Re: Converting transparent PNG

Post by emafuma »

Hi,
Thanks for replying, so this is the pic in question:
http://dl.dropbox.com/u/1069056/agrate_brianza.png
And this is the command line:
convert agrate_brianza.png -background transparent -transparent-color white -gravity Center -crop 150.0x150.0+0+0 -resize '136.0' -opaque white -fill white -transparent white -vignette 1x1+2+2 agg3.png

Thanks for any tips on that.
Bye
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Converting transparent PNG

Post by fmw42 »

Both these work for me:

First line: crop and resize your image
Second line: clone the result and extract the alpha channel
Third line: clone the alpha channel, colorize with black then draw a white circle
Fourth line: take the previous two (alpha channel) images and multiply them
Fifth line: put the multiplied alpha channel back into your cropped and resized image.


convert \( agrate_brianza.png -gravity Center -crop 150x150+0+0 -resize '136' \) \
\( +clone -alpha extract \) \
\( +clone -fill black -colorize 100% -fill white -draw "circle 68,68 68,136" \) \
\( -clone 1 -clone 2 -compose multiply -composite \) \
-delete 1,2 -alpha off -compose copy_opacity -composite agrate_brianza1.png


Same as above, but use vignette rather than drawing a circle

convert \( agrate_brianza.png -gravity Center -crop 150x150+0+0 -resize '136' \) \
\( +clone -alpha extract \) \
\( +clone -fill white -colorize 100% -background black -vignette 1x1+2+2 \) \
\( -clone 1 -clone 2 -compose multiply -composite \) \
-delete 1,2 -alpha off -compose copy_opacity -composite agrate_brianza2.png
emafuma
Posts: 9
Joined: 2011-07-15T05:45:37-07:00
Authentication code: 8675308

Re: Converting transparent PNG

Post by emafuma »

that works for me too! Thank you very much.
emafuma
Posts: 9
Joined: 2011-07-15T05:45:37-07:00
Authentication code: 8675308

Re: Converting transparent PNG

Post by emafuma »

I tried again, but the pic I get has transparent background while I want to have it white. Is it possible? Thanks
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Converting transparent PNG

Post by fmw42 »

add -compose over -background white -flatten just before your output image. see if that gets you what you want.

I will try to give you a shorter version later today.
emafuma
Posts: 9
Joined: 2011-07-15T05:45:37-07:00
Authentication code: 8675308

Re: Converting transparent PNG

Post by emafuma »

Thanks for replying
I did:
convert \( agrate_brianza.png -gravity Center -crop 150x150+0+0 -resize '136' \) \( +clone -alpha extract \) \( +clone -fill black -colorize 100% -fill white -draw "circle 68,68 68,136" \) \( -clone 1 -clone 2 -compose multiply -composite \) -delete 1,2 -alpha off -compose copy_opacity -composite -compose over -background white -flatten agrate_brianza89.png
but the resulting image is no longer a circle
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Converting transparent PNG

Post by fmw42 »

what color do you want outside the circle and what color inside the circle?
emafuma
Posts: 9
Joined: 2011-07-15T05:45:37-07:00
Authentication code: 8675308

Re: Converting transparent PNG

Post by emafuma »

It Should be white inside and tranparent outside (I mean it's the transparency that makes the pic looks circular)
Thanks
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Converting transparent PNG

Post by fmw42 »

Try this. If that is not what you want, then let me know further.


convert \( agrate_brianza.png -gravity Center -crop 150x150+0+0 +repage \
-resize 136 -background white -flatten \) \
\( +clone -fill black -colorize 100% -fill white -draw "circle 68,68 68,136" \) \
-alpha off -compose copy_opacity -composite agrate_brianza4.png


The +repage is needed to remove the virtual canvas so that the canvas size is the same as the image size at 150x150 so that your resize will end up 136x136 without distortion.

The -background white -flatten makes the background white. Then the circle mask the outside as transparent when put into the alpha channel.
Post Reply