Search code examples
perlimagemagickjpegperlmagick

How to "feather" the edges on image with PerlMagick


I have one image (JPEG) that I want to seamlessly superimpose on another. If I was trying to do this in Photoshop I would feather the edges. But I cannot work out how to achieve this with the PerlMagick api. I have tried using Vignette to create a fuzzy border, but that does not work as I would hope.

use Image::Magick;

$file = 'background.jpg';
$image = Image::Magick->new;
open(IMAGE, $file ) or die "Error cannot open file: $file"; 
$image->Read(file=>\*IMAGE);
close(IMAGE);

$file = 'face.jpg';
$face = Image::Magick->new;
open(IMAGE, $file ) or die "Error cannot open file: $file"; 
$face->Read(file=>\*IMAGE);
close(IMAGE);

$face->Vignette (geometry=>'5x5', radius=>50, x=>5, y=>5, background=>none);

$image->Composite(image=>$face,compose=>'hardlight',geometry=>'+480+800');

print "Content-type: image/jpeg\n\n";
binmode STDOUT;
$image->Write('jpg:-');

Solution

  • The hard edge is caused by the x=>5, y=>5, parameters. Remove these and the radius value, and the images will merge as required. The hardlight in combination with the vignette process creates and area where both images are mixed. So the code should be:

    use Image::Magick;
    
    $file = 'background.jpg';
    $image = Image::Magick->new;
    open(IMAGE, $file ) or die "Error cannot open file: $file"; 
    $image->Read(file=>\*IMAGE);
    close(IMAGE);
    
    $file = 'face.jpg';
    $face = Image::Magick->new;
    open(IMAGE, $file ) or die "Error cannot open file: $file"; 
    $face->Read(file=>\*IMAGE);
    close(IMAGE);
    
    $face->Vignette (geometry=>'5x5', background=>none);
    
    $image->Composite(image=>$face,compose=>'hardlight',geometry=>'+480+800');
    
    print "Content-type: image/jpeg\n\n";
    binmode STDOUT;
    $image->Write('jpg:-');