如何使用MagickWand叠加两个图像



我只是在看Raku的MagickWand到imagemagick:的接口

https://modules.raku.org/dist/MagickWand

我看不出有任何方法可以叠加两个图像。有一个示例/01-hello.pl6中演示的MagickWand.append-wands平铺图像,我看到代码中有一个蒙太奇方法(用于制作移动礼物?(,但我看不到像Flatten这样的东西我在perl的Image::Magick中使用过的方法。

我有一些使用MagickMergeImageLayers的代码,我对MagickWand进行了子类化,并添加了一个方法来完成它(下面是append-wands方法中的操作(。

这是子类模块:

use MagickWand;
use NativeCall;
use MagickWand::NativeCall;
use MagickWand::NativeCall::DrawingWand;
use MagickWand::NativeCall::Image;
use MagickWand::NativeCall::Mogrify;
use MagickWand::NativeCall::PixelIterator;
use MagickWand::NativeCall::PixelWand;
use MagickWand::NativeCall::Property;
use MagickWand::NativeCall::Wand;
use MagickWand::NativeCall::WandView;
use MagickWand::NativeCall::Deprecated;
use MagickWand::Enums;
class MagickWand::Doomed is MagickWand {
    submethod flatten-wands(+@wands) returns MagickWand {
        die "List must be defined with at least two elements" unless @wands.defined && @wands.elems >= 2;
        my $temp-wand = NewMagickWand;  # this "wand" is a cpointer
        MagickSetLastIterator($temp-wand);
        for @wands -> $wand {
            MagickAddImage($temp-wand, $wand.handle);  
            MagickSetLastIterator($temp-wand);
        }
        MagickSetFirstIterator($temp-wand);
        # an integer expected as second argument but the value
        # doesn't seem to do anything
        my $cloned-wand = MagickMergeImageLayers( $temp-wand, 0 );   
        DestroyMagickWand( $temp-wand );
        return MagickWand.new( handle => $cloned-wand );
    }
}

使用以上内容的一些示例脚本代码:

use MagickWand::Doomed;
my @images;  # stack of images to process ("wands", i.e. MagickWand objects)
my $bg = MagickWand::Doomed.new;
$bg.read( $input_image_file );
my ($w, $h) = ($bg.width, $bg.height);
$bg.label("conan_limits");
@images.push( $bg );
my    $overlay = MagickWand::Doomed.new;
$overlay.create( $w, $h, 'transparent' );  
$overlay.draw-line( 150, 120,  190, 70 );
$overlay.draw-line( 190, 70,   220, 120 );
$overlay.draw-line( 220, 120,  150, 120 );
$overlay.label("drawn");
@images.push( $overlay );
my $output_file = "$loc/flattened-output.png";
my $comparison = MagickWand::Doomed.flatten-wands( @images );
$comparison.write( $output_file );
# cleanup on exit
LEAVE {
  for @images -> $image {
    $image.cleanup   if $image.defined;  
  }
}

最新更新