【问题标题】:Applying alpha mask to image in Magick++在 Magick++ 中将 alpha 蒙版应用于图像
【发布时间】:2015-07-07 01:40:54
【问题描述】:

我有一个图像,它有两帧的序列。第二帧应该是第一帧的 alpha 蒙版。

这是一个例子:

http://i.imgur.com/c2M10u7.png

我使用 Magick++ 编写了以下代码,将图像分成两半,并应用 alpha 蒙版:

#include "stdafx.h"
#include <iostream>
#include <Magick++.h>

int main(int argc, char **argv)
{
    Magick::InitializeMagick(*argv);

    Magick::Image base, mask;
    std::string image;

    if (argc > 1)
    {
        image = argv[1];
    }
    else
        return EXIT_FAILURE;

    // Read image
    base.read(image);
    mask = base;

    // Crop out mask and sprite
    base.crop(Magick::Geometry(base.columns() / 2, base.rows(), 0, 0));
    mask.crop(Magick::Geometry(mask.columns() / 2, mask.rows(), mask.columns() / 2, 0));

    // Apply mask
    base.composite(mask, 0, 0, Magick::BlendCompositeOp);

    // Write
    base.write("output.png");

    return EXIT_SUCCESS;
}

但是,我不知道如何将它实际应用为 alpha 蒙版,而不仅仅是混合它。我也无法在任何地方找到解决方案,至少 C++ 没有。

任何帮助将不胜感激。

【问题讨论】:

  • 我觉得你需要CopyOpacityCompositeOp

标签: c++ imagemagick png magick++


【解决方案1】:

您应该禁用两个图像的 Alpha 通道并使用 CopyOpacity 而不是 Blend。它也看起来像你的面具是倒置的,所以你可能应该否定它。下面是一个 C++ 示例:

// Crop out mask and sprite
base.crop(Magick::Geometry(base.columns() / 2, base.rows(), 0, 0));
mask.crop(Magick::Geometry(mask.columns() / 2, mask.rows(), mask.columns() / 2, 0));

// Disable alpha channel
base.matte(false);
mask.matte(false);

// Invert the mask
mask.negate();

// Apply mask
base.composite(mask, 0, 0, Magick::CopyOpacityCompositeOp);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-05-15
    • 1970-01-01
    • 1970-01-01
    • 2016-04-11
    • 2017-10-07
    • 2011-03-22
    相关资源
    最近更新 更多