简体   繁体   English

剪切图像,使用PerlMagick在另一个图像上合成

[英]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. 我正在尝试剪切图像并将其放置在另一个图像上,例如蛋糕上的照片,我正在Perl中工作并使用Image :: Magick Perl库。 在此处输入图片说明 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. 另外,我看到了一些使用AffineTransform的帖子,但是使用这些帖子我无法实现剪切效果。

I solved it by creating a mask ( by inverting alpha channel ). 我通过创建遮罩(通过反转alpha通道)解决了问题。 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: 最终图片: 在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用 PerlMagick “羽化”图像边缘 - How to “feather” the edges on image with PerlMagick 向使用 PerlMagick 读取的图像添加 Alpha 通道 - Add an alpha channel to an image read in with PerlMagick Image::Magick (perlmagick) 调整纵横比和质量问题(不同于转换命令行实用程序) - Image::Magick (perlmagick) resizing aspect ratio and quality issues (different than convert command line utility) 如何使用PerlMagick从现有图像对象而不是文件路径“读取”? - How do I “read” from an existing image object instead of a file path with PerlMagick? 如何使用Homebrew在Mac OSX上安装PerlMagick? - How to install PerlMagick on Mac OSX using Homebrew? 在比较API中将PerlMagick与Fuzz参数一起使用 - Using fuzz parameter with perlmagick in compare api Image::Magick Composite() 和 Gimp 图层叠加的区别 - Image::Magick Composite() and Gimp layer overlay difference 如何使用PerlMagick提取EXIF数据? - How can I extract EXIF data using PerlMagick? 如何使用PerlMagick替换任何非特定颜色的颜色? - How do I replace any color that is not a specific color using PerlMagick? 为什么Perl的Image :: Magick在半透明图像周围创建轮廓但复合图像不是? - Why does Perl's Image::Magick create an outline around a semi-transparent image but composite doesn't?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM