Re: Compositing transparent png over image using Magick++ GraphicsMagick API

Dragan Jotanovic <[email protected]> Thu, 30 Jan 2014 17:04:58 +0000
Newsgroups gmane.comp.video.graphicsmagick.apis
Message-ID <CAJSsW4rptandQOf9EOVV8JSztOtcWZ-1vP4y-OBM0s-dhdo01g@mail.gmail.com>
Well, I finally managed to make the watermark transparent. It seems that
there is no high level function to set opacity properly so I had to
manipulate PixelPacket array directly.

The final code is:

  Image rose("magick:rose");
  Image watermark("watermark.png");
  int opacity = 50 * MaxRGB / 100;
  PixelPacket *pixels = watermark.getPixels(0, 0, watermark.columns(),
watermark.rows());
  for (int j = 0; j < watermark.rows(); j++) {
  for (int i = 0; i < watermark.columns(); i++) {
  int ind = i*watermark.columns() + j;
  if (pixels[ind].opacity != MaxRGB) { // skip transparent pixels
  pixels[ind].opacity = opacity;
  }
  }
  }
  watermark.syncPixels();
  rose.composite(watermark, CenterGravity, AtopCompositeOp);
  rose.write("destination.gif");

The only composite operator that works properly for images with
transparency is Atop (this is just for Magick++ API).
It's a shame that other compositing operators just don't work properly with
images containing transparency.

Dragan




On Wed, Jan 29, 2014 at 3:25 PM, Dragan Jotanovic <[email protected]> wrote:

> Ok, good. I got proper overlay with OverCompositeOp:
>
>   Image rose("magick:rose");
>   Image watermark("watermark.png");
>   rose.composite(watermark, CenterGravity, OverCompositeOp);
>   rose.write("destination.gif");
>
> Now I need to apply opacity to watermark, so that background image could
> be seen through the watermark.
> I tried setting watermark.opacity(MaxRGB*30/100); but transparent color is
> again lost.
>
> Basically command line that I want to implement in Magick++ is this:
>
> gm composite -dissolve 50 -gravity center watermark.png magick:rose
> destination.gif
>
> I tried to do it with this code:
>
>     Image rose("magick:rose");
>   Image watermark("watermark.png");
>   watermark.opacity(MaxRGB*30/100);
>   rose.composite(watermark, CenterGravity, DissolveCompositeOp);
>   rose.write("destination.gif");
>
> but the result is not comparable (ie. letter A is transparent but the
> background from watermark is black with some transparency, not clear
> transparent).
>
> Do you know any way how to accomplish this? I just need semi transparent
> watermark overlayed on source image.
>
>
> On Wed, Jan 29, 2014 at 2:59 PM, Bob Friesenhahn <
> [email protected]> wrote:
>
>> On Wed, 29 Jan 2014, Dragan Jotanovic wrote:
>>
>>>
>>>     Image rose("magick:rose");
>>>   Image watermark("watermark.png");
>>>   rose.composite(watermark, CenterGravity, CopyCompositeOp);
>>>   rose.write("destination.gif");
>>>
>>> but I get black pixels in place of transparent pixels from watermark.png.
>>> I also tried setting rose.matte(true); and watermark.matte(true);
>>> without results.
>>>
>>> Is there an easy way to accomplish this?
>>>
>>
>> It seems that I provided wrong information about the default composite
>> operator.  The GM manual page says that 'Over' is the default. Please use
>> OverCompositeOp or AtopCompositeOp rather than CopyCompositeOp and then you
>> should be happy.
>>
>> The command line is apparently not using CopyCompositeOp.
>>
>> You can test from the command line like:
>>
>>   gm composite -compose Atop -gravity center watermark.png rose:
>> destination.gif
>>
>>   gm composite -compose Over -gravity center watermark.png rose:
>> destination.gif
>>
>> vs (wrong)
>>
>>   gm composite -compose Copy -gravity center watermark.png rose:
>> destination.gif
>>
>>
>> Bob
>> --
>> Bob Friesenhahn
>> [email protected], http://www.simplesystems.org/
>> users/bfriesen/
>> GraphicsMagick Maintainer,    http://www.GraphicsMagick.org/
>>
>>
>> ------------------------------------------------------------------------------
>> WatchGuard Dimension instantly turns raw network data into actionable
>> security intelligence. It gives you real-time visual feedback on key
>> security issues and trends.  Skip the complicated setup - simply import
>> a virtual appliance and go from zero to informed in seconds.
>>
>> http://pubads.g.doubleclick.net/gampad/clk?id=123612991&iu=/4140/ostg.clktrk
>> _______________________________________________
>> Graphicsmagick-apis mailing list
>> [email protected]
>> https://lists.sourceforge.net/lists/listinfo/graphicsmagick-apis
>>
>>
>

------------------------------------------------------------------------------
WatchGuard Dimension instantly turns raw network data into actionable 
security intelligence. It gives you real-time visual feedback on key
security issues and trends.  Skip the complicated setup - simply import
a virtual appliance and go from zero to informed in seconds.
http://pubads.g.doubleclick.net/gampad/clk?id=123612991&iu=/4140/ostg.clktrk

_______________________________________________
Graphicsmagick-apis mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/graphicsmagick-apis