【问题标题】:Remove background after comparing two images with Imagick将两个图像与 Imagick 进行比较后删除背景
【发布时间】:2015-01-23 12:18:45
【问题描述】:

我是使用 imagick 和 php 的新手,我遇到了一个问题: 我正在使用 compareImages() 函数比较两个图像。我使用与documentation page 中相同的代码:

<?php

$image1 = new imagick("image1.png");
$image2 = new imagick("image2.png");

$result = $image1->compareImages($image2, Imagick::METRIC_MEANSQUAREERROR);
$result[0]->setImageFormat("png");

header("Content-Type: image/png");
echo $result[0];

?>

此代码运行正常,但结果图像的背景与原始图像有一点不透明度。

我一直在搜索,但使用 imagemagick 命令找到了我想要的结果: http://www.imagemagick.org/Usage/compare/

我当前的结果类似于第一个命令的图像(“compare bag_frame1.gif bag_frame2.gif compare.gif”),我想要一个类似于此命令所示的结果:

 compare bag_frame1.gif bag_frame2.gif \
      -compose Src compare_src.gif

有没有办法用 imagick 的 compareImages() 函数做到这一点?

谢谢

【问题讨论】:

    标签: php imagemagick imagick


    【解决方案1】:

    在阅读第一张图片之前,您需要使用Imagick::SetOption

    <?php
    $image1 = new imagick(); // Init empty object
    $image2 = new imagick("image2.png");
    // Set Lowlight (resulting background) to transparent, not "original image with a bit of opacity"
    $image1->setOption('lowlight-color','transparent');
    // Switch the default compose operator to Src, like in example
    $image1->setOption('compose', 'Src');
    // Now read image
    $image1->readImage("image1.png");
    // Result will not have a background
    $result = $image1->compareImages($image2, Imagick::METRIC_MEANSQUAREERROR);
    

    【讨论】:

    • 如果我将 Lowlight 设置为透明,我会得到黑色背景,而不是透明 1。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-06-02
    • 2019-07-07
    • 2011-09-12
    • 2013-11-30
    • 2019-04-16
    • 2014-04-01
    • 2013-10-19
    相关资源
    最近更新 更多