Why does Perl Image :: Magick create an outline around the semitransparent image, but the composite doesn't work?

I am working on a program to dynamically add watermarks to product images using Image :: Magic . I am using composite

using the method dissolve

. The watermark image is a PNG with transparency. It works on Linux with ImageMagick 6.7.6-9 2012-05-16 Q16

.

Consider the following arbitrary examples of images:

Background ( test.jpg

):

Background (product image)

Watermark / Overlay ( example.png

):

Example watermark

If I put them along with the command line tool composite

everything works fine.

composite -dissolve 60% -gravity center example.png test.jpg out.jpg

      

Correct output of CLI

The text (this should be an image, it will contain graphics) is superimposed over the background. The edges are how they were in the original watermark.

#!/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:

Weird output of Perl program

As you can see, the new image has some strange edges, almost like an outline. The larger this image (original source images are> 1000px), the more obvious this outline becomes.

Here's a close-up:

Closup of correct image

Closeup of incorrect image

I believe it may have something to do with the strength of the JPEG compression, because the wrong image has a lot more artifacts. But that would mean Perl Image :: Magick and CLI defaults. I haven't figured out how to set up compression yet.

Anyway, I would appreciate any input on why this might be happening, or ideas and suggestions on how to get rid of it.

+3


source to share


1 answer


I quickly looked at the source code PerlMagick and it seems that Composite

a Dissolve

mistake when 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',
);

      

+1


source







All Articles