Pradeep
Pradeep

Reputation: 3153

Shear image, composite on another image, using PerlMagick

I am trying to shear an image and place on another image, like a photo on a cake, I am working in Perl and using Image::Magick Perl library.enter image description here Here is the output I am able to generate, I am unable to make the background of the sheared image. Here's my code:

#!/usr/bin/perl
use strict;
use warnings;
use Image::Magick;

my $im = Image::Magick->new;

$im->Read('cake.jpg');

$im->Scale('50%');

my $sh = Image::Magick->new;

$sh->Set( magick => 'png' );

$sh->Read('oo.jpg');

$sh->Resize(width => 580, height => 540);

$sh->Shear( geometry => '580x540+0+0', x => -40, y => -30, 'virtual-pixel' => 'Transparent');

$im->Composite( image => $sh, geometry => '+90+60');

$im->Write('test.jpg');

Also, I have seen some posts using AffineTransform, but using those I am unable to achieve the shear.

Upvotes: 1

Views: 241

Answers (1)

Pradeep
Pradeep

Reputation: 3153

I solved it by creating a mask ( by inverting alpha channel ). Here's the code, hope this helps someone:

use strict;
use warnings;
use Image::Magick;

my $im = Image::Magick->new;

$im->Read('cake.jpg');

$im->Scale('50%');

my $sh = Image::Magick->new;

$im->Set( magick => 'png' );
$sh->Set( magick => 'png' );

$sh->Read('oo.jpg');
$sh->Scale('50%');

## create a copy
my $sh1 = $sh->Clone();

## set alpha
$sh->Set('alpha' => 'Transparent');

## shear first
my $x = $sh->Shear( geometry => '290x270+0+0', x => -40, y => -30, fill => undef);

## invert to get a mask
$sh->Negate(channel => 'Alpha');

## shear copy
$x = $sh1->Shear( geometry => '290x270+0+0', x => -40, y => -30);

## apply mask
$sh1->Composite( compose => 'CopyOpacity', image => $sh);

$im->Composite( image => $sh1, geometry => '+450+260', );

$im->Write('test.jpg');

Final image: enter image description here

Upvotes: 2

Related Questions