imagemagick - 如何使用 MagickWand 叠加两个图像?

标签 imagemagick raku

我只是在看 Raku 的 MagickWand imagemagick 界面:
https://modules.raku.org/dist/MagickWand
而且我看不到任何叠加两个图像的方法。有一个
示例/01-hello.pl6 中演示的 MagickWand.append-wands
平铺图像,我看到代码中有一个蒙太奇方法
(用于创建动态 gif?),但我没有看到像 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;  
  }
}

关于imagemagick - 如何使用 MagickWand 叠加两个图像?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64526060/

相关文章:

PHP-Imagemagick图像显示

recursion - 在硬币变化类型的问题中将递归函数重构为迭代

perl - 如何从 perl6 中的数组或散列创建方法

multidimensional-array - perl6 : access values in a multidimensional variable

java - ImageMagick在Spring boot的打包WAR中找不到convert.exe的路径

imagemagick - 图像魔术: resize and fill doesn't work

raku - 如何将超算子与非标量的标量一起使用?

Raku/Perl6 : how do you code a NULL with NativeCall

image-processing - 哪一边向上? - 自动图像旋转算法

imagemagick - 将 PDF 转换为 JPG - 两页跨页?