MagickSetImageOpacity behavior

The MagickWand interface is a new high-level C API interface to ImageMagick core methods. We discourage the use of the core methods and encourage the use of this API instead. Post MagickWand questions, bug reports, and suggestions to this forum.
Post Reply
aidynskas

MagickSetImageOpacity behavior

Post by aidynskas »

Hi,

Anoyne could explain me how do I accomplish layer opacity settings similar to Photoshop/GIMP? PNG and PSD files load just fine with their corresponding opacity values. However what I want to do is control their % opacity like 0%,34%,100%,etc. Unfortunately MagickSetImageOpacity has no effect if i call it on image, that has matte enabled (nothing happens from visual perspective at least). Disabling matte and then setting the opacity does the work exactly how I want it to be done, but matte channel is lost and black garblish around the image appears.

Anyone has suggestions how to accomplist what I want?

Thanks a lot!
User avatar
magick
Site Admin
Posts: 11064
Joined: 2003-05-31T11:32:55-07:00

Re: MagickSetImageOpacity behavior

Post by magick »

Are you using a normalized value such as MagickSetImageOpacity(..., 0.34) for 34% transparency?
aidynskas

Re: MagickSetImageOpacity behavior

Post by aidynskas »

Yes, the value is in range of [0;1]. I will code some quick example on how it works and add it soon.
aidynskas

Re: MagickSetImageOpacity behavior

Post by aidynskas »

Here is the complete sample code:

Code: Select all

MagickWand *magick_wand=NewMagickWand();
	MagickReadImage(magick_wand,"/test.png");
		
	MagickSetImageOpacity(magick_wand,0.5);

	MagickWriteImage(magick_wand,"/result.png");
1. The test.png image can be found at http://www.jumsoft.com/images/test.png
2. Result image with matte setting unchanged can be found at http://www.jumsoft.com/images/result_matteistrue.png
3. Result image with matte setting changed to false can be found at http://www.jumsoft.com/images/result_matteisfalse.png

Any ideas?

Thanks a lot,
Aidas
User avatar
magick
Site Admin
Posts: 11064
Joined: 2003-05-31T11:32:55-07:00

Re: MagickSetImageOpacity behavior

Post by magick »

We can reproduce the problem and will have a fix in the latest release of ImageMagick by tommorrow. Thanks.
aidynskas

Re: MagickSetImageOpacity behavior

Post by aidynskas »

Great news!

Thank you very much!
aidynskas

Re: MagickSetImageOpacity behavior

Post by aidynskas »

magick wrote: We can reproduce the problem and will have a fix in the latest release of ImageMagick by tommorrow. Thanks.


Sorry for bumping this post, but I wonder if the bug is fixed yet (I've installed latest version and it seems that the problem is still there)?

Maybe I could help identifying the problem?
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: MagickSetImageOpacity behavior

Post by anthony »

The problem is that that function is only used to initialize a matte channel, when none exists. I know I just looked at the code for another problem.

I gather you just want to half the opacity of an existing image, that already has some transparency. If so you best use some channel limits function to multiply the alpha values by .5

On the command line that would be "-channel A -evaluate divide 2"
You could also use a Composite Dissolve function too, which does similar alpha channel modifications before overlaying images.

Sorry I do not know magick++ to help you code in in that API.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
mrBlaQ
Posts: 17
Joined: 2008-02-05T12:19:00-07:00

Re: MagickSetImageOpacity behavior

Post by mrBlaQ »

The equivalent MagickWand PHP syntax is the following

Code: Select all

MagickEvaluateImage($wnd, MW_DivideEvaluateOperator, (100/$alpha), MW_AlphaChannel);
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: MagickSetImageOpacity behavior

Post by anthony »

You could also use Multiply too, assuming that $alpha is a percentage.

Code: Select all

MagickEvaluateImage($wnd, MW_MultiplyEvaluateOperator, ($alpha/100), MW_AlphaChannel);
That avoids any chance of a division by zero error.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
Post Reply