Reputation: 54323
I'm working on a program to dynamically add watermarks to product images with Image::Magick. I'm using composite
with the dissolve
method. The watermark image is a PNG that has transparency. It's running on Linux with ImageMagick 6.7.6-9 2012-05-16 Q16
.
Consider the following arbitrary example images:
test.jpg
):example.png
):If I put these together with the command line tool composite
, everything is great.
composite -dissolve 60% -gravity center example.png test.jpg out.jpg
The text (this needs to be an image, there will be graphics in it, too) is superimposed over the background. The edges are just the way they were in the original watermark image.
#!/usr/bin/perl
use strict; use warnings;
use Image::Magick;
# this objects represents the background
my $background = Image::Magick->new;
$background ->ReadImage( 'test.jpg' );
# this objects represents the watermark
my $watermark = Image::Magick->new;
$watermark->ReadImage( 'example.png');
# there is some scaling going on here...
# both images are scaled down to have the same width
# but the problem occurs even if I turn the scaling off
# superimpose the watermark
$background->Composite(
image => $watermark,
compose => 'Dissolve',
opacity => '60%',
gravity => 'Center',
);
$background->Write( filename => 'out.jpg' );
Here's the output of this Perl program:
As you can see, the new image has some strange edges, almost like an outline. The larger this image gets (the original source images are both > 1000px) the more obvious this outline becomes.
Here's a closeup:
I believe it might have something to do with the strength of the JPEG compression because the wrong image has a lot more artefacts. But that would mean the defaults of Perl's Image::Magick and the CLI are different. I have not figured out how to set the compression yet.
Anyway, I'd be glad about any kind of input on why this might happen, or ideas and suggestions on how to get rid of it.
Upvotes: 3
Views: 859
Reputation: 33618
I had a quick look at the PerlMagick source code and it seems that Composite
with Dissolve
is buggy when the dissolved image has an alpha channel. The following works for me:
$watermark->Evaluate(
operator => 'Multiply',
value => 0.6,
channel => 'Alpha',
);
$background->Composite(
image => $watermark,
gravity => 'Center',
);
Upvotes: 1