perlimagemagickperlmagick

How to composite watermark with Perlmagick?


I wrote a script to watermark images. On command line adding watermark works fine:

composite -dissolve 5% -gravity South watermark.png original.jpg new.jpg

Now when I tried to use Perlmagick for watermarking, the composite part does not change anything:

my $i = new Image::Magick;
my $watermark = $i->Read( "watermark.png" );

my $i2 = new Image::Magick;
my $p = $i->Read( "original.jpg" );

$i->Set( Gravity => 'Center' );
$i->Resize( geometry => "600x600" );
$i->Composite( image=>$watermark, compose=>"Dissolve", opacity=>5, gravity=>"South" );

$i->Write( 'jpg:temp_file' );

Such script resizes image, but does not watermark it.

Problem seems to be in the way I provide dissolving degree. On command line I can add percentage to dissolve-attribute, but in API version dissolve is itself attribute. I tried with opacity, but this seems not the right way too.

What is the right way to use dissolve in Perlmagick?

Edit: Images I use

original

watermark

new (result of command composite -dissolve 5% -gravity South watermark.png original.jpg new.jpg)


Solution

  • I tried the following:

    use strict;
    use warnings;
    use feature qw(say);
    use Image::Magick;
    
    my $watermark = Image::Magick->new();
    {
        my $error = $watermark->Read( "watermark.png" );
        die "Cannot read watermark.png: $error" if $error;
    }
    my $original = Image::Magick->new();
    {
        my $error = $original->Read( "original.jpg" );
        die "Cannot read original.jpg: $error" if $error;
    }
    
    #$watermark->Set( Gravity => 'Center' );
    #$watermark->Resize( geometry => "600x600" );
    {
        my $error = $original->Composite(
            image=>$watermark,
            compose=>"dissolve",
        #    args => "90",
            opacity=>"2%",
            gravity=>"South"
        );
        die "Cannot do composite: $error" if $error;
    }    
    {
        my $error = $original->Write( 'new.jpg' );
        die "Cannot write new.jpg: $error" if $error;
    }
    say "Done.";
    

    It gives me:

    enter image description here

    So you can see the watermark has been composed into the original. So maybe the opacity parameter can be used like this?