duanchique1196
2016-04-04 15:42
浏览 579
已采纳

ImageMagick / Imagick:将带有Alpha通道的PNG转换为2色图像(颜色,透明)

I would like to know and find out, how I can colorize/replace any pixel of an image, that is not (fully) transparent with an opaque pixel.

For example, having a multicolored Logo with transparent pixels, I would like to convert it in to a logo with only the color #ff0000, and not change the transparent background.

I want to achieve this with the PHP Imagick Library. I cannot find any good documentation.

I thought that Imagick::thresholdImage would be a helper, but there is no documentation about the threshold parameter.

Best results are achieved with this fragment of code. But still not working perfectly. Some pixels - i guess those with alpha > 0 and < 1 are not replaced.

$image = new \Imagick($source);
$image->setImageFormat('png');

$fill = new \ImagickPixel('#ff0000');
$image->thresholdImage(0);
$image->paintOpaqueImage('#ffffff', $fill, 1);

$image->writeImage($destination);

图片转代码服务由CSDN问答提供 功能建议

我想知道并了解如何着色/替换图像的任何像素,这不是 (完全)透明的不透明像素。

例如,有一个带有透明像素的多色徽标,我想将其转换为仅带有#ff0000颜色的徽标,而不是 改变透明背景。

我想通过PHP Imagick Library实现这一目标。 我找不到任何好的文档。

我认为 Imagick :: thresholdImage 将是一个帮手,但没有关于阈值参数的文档。

使用此代码片段可以获得最佳结果 。 但仍然没有完美的工作。 一些像素 - 我猜那些有alpha&gt; 0和&lt; 1不会被替换。

  $ image = new \ Imagick($ source); 
 $ image-&gt; setImageFormat('png'); 
 
 $ fill  = new \ ImagickPixel('#ff0000'); 
 $ image-&gt; thresholdImage(0); 
 $ image-&gt; paintOpaqueImage('#ffffff',$ fill,1); 
 
 $ image-  &gt; writeImage($ destination); 
   
 
  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • dongsun2789 2016-04-04 19:09
    已采纳

    I would like to know and find out, how I can colorize/replace any pixel of an image, that is not (fully) transparent with an opaque pixel.

    You almost certainly don't.

    The code below does what you are asking (I think) and the output looks terrible. Perhaps you should give an example input image, and a hoped for example output image, that you have edited in Photoshop, to show what you were hoping for.

    Input image:

    enter image description here

    Output image:

    enter image description here

    $imagick = new Imagick("fnord.png");
    
    // Get the alpha channel of the original image.
    $imagick->separateImageChannel(\Imagick::CHANNEL_ALPHA);
    
    // Make all the colors above this pure white.
    $imagick->whiteThresholdImage("rgb(254, 254, 254)");
    
    // Make all the colors below this pure black.
    $imagick->blackThresholdImage("rgb(254, 254, 254)");
    
    // We want the mask the other way round
    $imagick->negateImage(false);
    
    $imagickCanvas = new \Imagick();
    
    $imagickCanvas->newPseudoImage(
        $imagick->getImageWidth(),
        $imagick->getImageHeight(),
        "xc:rgb(255, 0, 0)"
    );
    
    // Copy the mask back as the alpha channel.
    $imagickCanvas->compositeImage($imagick, \Imagick::COMPOSITE_COPYOPACITY, 0, 0);
    
    // Write out the image.
    $imagickCanvas->setImageFormat('png');
    $imagickCanvas->writeImage("./output.png");
    
    评论
    解决 无用
    打赏 举报