1

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.

Pradeep
  • 3,093
  • 17
  • 21
  • What is the question? Do you want the background to be transparent? In place of setting the virtual-pixel to transparent, set the background color to transparent. – fmw42 Aug 20 '19 at 16:43
  • Yes I want the background to be transparent, but the Shear method doesnt accept the virtual-pixel argument – Pradeep Aug 21 '19 at 04:17
  • Try setting the background color not the virtual-pixel to transparent. – fmw42 Aug 21 '19 at 04:19
  • Shear method doesnt accept the virtual-pixel argument, *Shear geometry=>geometry, x=>double, y=>double fill=>color name* – Pradeep Aug 22 '19 at 08:30
  • As I keep saying, the virtual pixel setting has no affect in shear. It is the background color that needs to be set. Use the color name argument or set the background color attribute before calling shear. – fmw42 Aug 22 '19 at 15:44
  • @fmw42 as I mentioned in my previous comment, there is no background parameter, but if I pass the available fill parameter as *fill => 'transparent'* the background becomes black. I have solved the problem by creating a mask. – Pradeep Aug 23 '19 at 08:10

1 Answers1

2

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

Pradeep
  • 3,093
  • 17
  • 21