c++ - How to set opacity to png image with transparent background using Magick++ -


i'm using imagemagick render images. open png file magick::image , draw on magick::image , set opacity png image. , save jpg file.

in saved file transparent background becomes black.

example code:

    image newimage;     newimage.size(geometry(1000, 1000));     newimage.fillcolor(color(50, transparentopacity / 2, 50));     newimage.draw(drawablerectangle(0, 0, 1000, 1000));      image originalimage("test-image-1.png");     originalimage.opacity(transparentopacity / 2);     newimage.composite( originalimage, 300, 100, atopcompositeop );      newimage.magick("jpg");     newimage.write("testimage3.jpg"); 

is possible set transparency 50% image , full transparent background?

the issue line:

originalimage.opacity(transparentopacity / 2); 

the source, "test-image-1.png", has alpha-channel looks like...

orignal alpha channel

when set opacity 50%, you're setting whole channel, not reducing level 50%. alpha-channel altered originalimage.opacity looks looks this...

50% alpha-channel

there many ways alter alpha-channel reduce image opacity. pixel iteration, fx, , level color name few. i'm fan of isolating alpha channel, altering levels, , coping channel image. example below "swaps" color values 50% opacity == gray50.

image originalimage("test-image-1.png"); image mask(originalimage); // clone image mask.channel(opacitychannel); // isolate alpha-channel /*   example i'll mimic cli options:   "-fuzz 50% -fill gray50 -opaque black" */ mask.colorfuzz(maxrgb * 0.5); mask.opaque(color("black"), color("gray50")); mask.negate(); // copy mask image new alpha-channel originalimage.composite( mask, 0, 0, copyopacitycompositeop ); 

reduced opacity 50%

now can composite on image without worry of black background.


Comments

Popular posts from this blog

Java 3D LWJGL collision -

spring - SubProtocolWebSocketHandler - No handlers -

methods - python can't use function in submodule -