how to change the transparency(opacity) of an image

Questions and postings pertaining to the development of ImageMagick, feature enhancements, and ImageMagick internals. ImageMagick source code and algorithms are discussed here. Usage questions which are too arcane for the normal user list should also be posted here.
Post Reply
hakiim35
Posts: 19
Joined: 2010-02-18T07:04:01-07:00
Authentication code: 8675308

how to change the transparency(opacity) of an image

Post by hakiim35 »

Hello, I have a png image and I want to change its alpha channel transparency.
There are some functions such as opacity(), backgroundColor(), transparent(), etc.
Suppose that I m receiving the transparency threshold(0-255) from the user in runtime.
Which one to use and how? should I write the image to a Blob and read back again to Image so that the change applies in runtime?

Best.
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 transparency(opacity) of an image

Post by fmw42 »

need more information about what is in the alpha channel and how you want to change it.

basically extract the alpha channel (-alpha extract), modify it, then reattach it (-compose copy_opacity -composite)

There are other ways to do it all in one command line, but I need to know more about what you are doing.
hakiim35
Posts: 19
Joined: 2010-02-18T07:04:01-07:00
Authentication code: 8675308

Re: how to change the transparency(opacity) of an image

Post by hakiim35 »

I am developing an image processing tool that allows users to read/manipulate different image formats and set/change the attributes of them. I am using magick++ api, so command line processing is not the way I need. Basically, I want to enable the user to change the transparency of a png file. I tried setting the background attribute by

Code: Select all

int alpha = getThreshold(); Color c(0,0,0,alpha); image.backgroundColor(c);
but it didnt change anything. I also set the opacity by

Code: Select all

int c = getThreshold(); image.opacity(c);
but it didnt help either..hope it is clear now, thanks for your interest.
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 transparency(opacity) of an image

Post by fmw42 »

I don't use API, only command line. But you have not said how you want to change the transparency. Do you want to set the whole channel to the same value or something else.
hakiim35
Posts: 19
Joined: 2010-02-18T07:04:01-07:00
Authentication code: 8675308

Re: how to change the transparency(opacity) of an image

Post by hakiim35 »

I want to set the channel to same value.
Thanks.
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 transparency(opacity) of an image

Post by fmw42 »

In command line it could be done as either

convert zelda3.png -alpha on -channel A -evaluate set 25% +channel tmp2.png

or

convert image.png \( +clone -fill "gray(25%)" -colorize 100 \) -alpha off -compose copy_opacity -composite tmp3.png


This sets the opacity to 25% (transparency to 75%). So it is mostly transparent.


Sorry I don't know how to do it in any API.
hakiim35
Posts: 19
Joined: 2010-02-18T07:04:01-07:00
Authentication code: 8675308

Re: how to change the transparency(opacity) of an image

Post by hakiim35 »

Thanks..
anybody to offer a solution using magick++ ?

Best.
Post Reply