2

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:-');
Melchester
  • 421
  • 3
  • 18
  • *"I would feather the edges.."* Do you mean fading like [this](https://legacy.imagemagick.org/discourse-server/viewtopic.php?t=33597) ? – Håkon Hægland Jul 28 '21 at 14:19
  • No, I want the edge of the smaller image to be blended with the background image, so there is not a straight edge, but one image fades into the other. I thought that the vignette effect combined with the hard light might achieve that, but I still get a hard edge around the smaller image. Thanks for taking a look. – Melchester Jul 28 '21 at 21:33
  • Ok, can you upload the sample images somewhere? Also it would be nice to have an image showing the expected result. – Håkon Hægland Jul 29 '21 at 12:05
  • 1
    So sorry to have wasted your time. I have discovered the problem. In the Vingnette command I had "radius=>50, x=>5, y=>5" which was creating a hard line around the edge of the image. I did not understand what these elements do. When I removed them I get the effect I wanted/expected. – Melchester Jul 29 '21 at 21:37

1 Answers1

1

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:-');
Melchester
  • 421
  • 3
  • 18