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

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问答提供 功能建议

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

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

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

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

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

  $ 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); 
 </ code> </ pre> 
 </ div>

1条回答 默认 最新

相关推荐 更多相似问题