3

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:

Background (test.jpg):

Background (product image)

Watermark/overlay (example.png):

Example watermark

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

Correct output of CLI

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:

Weird output of 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:

Closup of correct image

Closeup of incorrect image

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.

4
  • JPEG/MIFF/PNG compression level is set via the quality option.
    – Perleone
    Jan 28, 2013 at 14:58
  • I tried values ranging from 80 to 100, but that didn't change anything.
    – simbabque
    Jan 28, 2013 at 17:00
  • 1
    No such problems with GraphicsMagick -- it's designed as a stable and more secure drop-in replacement for ImageMagick.
    – Perleone
    Jan 28, 2013 at 20:43
  • 1
    It does seem to be an issue with the PNG->JPEG conversion. I'd suggest trying the Resample method (info here) and setting a high density for the watermark, possibly along with setting blur < 1 for sharpness. Jan 29, 2013 at 17:05

1 Answer 1

1

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',
);
3
  • This solved the problem. Thanks a lot. Would you mind adding a little more info on what this does?
    – simbabque
    Feb 6, 2013 at 15:50
  • The Dissolve operation draws an image with a custom opacity on top of another. The code I posted executes this operation in two steps. First the opacity of the watermark is changed by multiplying the alpha channel with a constant value. Then the watermark is drawn on top of the background using Composite with the default Over method.
    – nwellnhof
    Feb 6, 2013 at 16:51
  • Ah, I see. Thank you. I did not copy the second part. Turns out, it even works with the Dissolve still in the Composite call. Just doing the Multiply changed something with the watermark that made it work.
    – simbabque
    Feb 6, 2013 at 17:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.