简体   繁体   中英

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. 在此处输入图片说明 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.

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: 在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM