我想将图片转换为只有两种颜色,黑色和白色的GIF格式
因此输出图像中的每个像素都将是黑色或白色
有人知道以任何方式转换成想象力吗?
答案 0 :(得分:3)
虽然写出双色gif的最终转换是相同的,但是将图像转换为黑白两种颜色的方式略有不同。
以下是一些方法:
辅助方法实际强制图像为值为0,0,0和255,255,255的像素
function forceBlackAndWhite(Imagick $imagick, $ditherMethod = \Imagick::DITHERMETHOD_NO)
{
$palette = new Imagick();
$palette->newPseudoImage(1, 2, 'gradient:black-white');
$palette->setImageFormat('png');
//$palette->writeImage('palette.png');
// Make the image use these palette colors
$imagick->remapImage($palette, $ditherMethod);
$imagick->setImageDepth(1);
}
只需使用重映射到调色板即可将图像强制为2种颜色而不会产生任何抖动。
function twoColorPaletteOnly()
{
$imagick = new Imagick(__DIR__."/../images/Biter_500.jpg");
forceBlackAndWhite($imagick, \Imagick::DITHERMETHOD_NO);
$imagick->setImageFormat('gif');
$imagick->writeImage("./outputPalette.gif");
}
调色板输出:
使用http://phpimagick.com/Imagick/posterizeImage可以对抖动过程进行不同的控制。
function twoColorViaPosterize()
{
$imagick = new Imagick(__DIR__."/../images/Biter_500.jpg");
$imagick->transformImageColorspace(\Imagick::COLORSPACE_GRAY);
$imagick->posterizeImage(2, \Imagick::DITHERMETHOD_RIEMERSMA);
forceBlackAndWhite($imagick);
$imagick->setImageFormat('gif');
$imagick->writeImage("./outputPosterize.gif");
}
海报化输出:
thresholdImage功能允许我们控制在'级别'图像从黑色变为白色。
function twoColorViaThreshold()
{
$imagick = new Imagick(__DIR__."/../images/Biter_500.jpg");
$imagick->transformImageColorspace(\Imagick::COLORSPACE_GRAY);
$imagick->thresholdImage(0.5 * \Imagick::getQuantum());
forceBlackAndWhite($imagick);
$imagick->setImageFormat('gif');
$imagick->writeImage("./outputThreshold.gif");
}
阈值输出:
使用blackThresholdImage和whiteThresholdImage函数可以控制每个通道的颜色阈值
function twoColorViaColorThreshold()
{
$imagick = new Imagick(__DIR__."/../images/Biter_500.jpg");
$thresholdColor = "RGB(127, 100, 100)";
$imagick->blackThresholdImage($thresholdColor);
$imagick->whiteThresholdImage($thresholdColor);
forceBlackAndWhite($imagick);
$imagick->setImageFormat('gif');
$imagick->writeImage("./outputColorThreshold.gif");
}
colorThreshold输出
提取单个图片频道可以产生一个“清洁工”。看输出图像。
function twoColorViaColorChannelThreshold()
{
$imagick = new Imagick(__DIR__."/../images/Biter_500.jpg");
$imagick->separateImageChannel(\Imagick::CHANNEL_RED);
$imagick->thresholdImage(0.5 * \Imagick::getQuantum());
forceBlackAndWhite($imagick);
$imagick->setImageFormat('gif');
$imagick->writeImage("./outputColorChannelThreshold.gif");
}
colorChannelThreshold
我们可以使用colorMatrixImage函数更精确地组合RGB通道,这使我们可以完全控制单独的R G B值如何影响输出图像。
function twoColorViaColorMatrixChannelThreshold()
{
$imagick = new Imagick(__DIR__."/../images/Biter_500.jpg");
// The only
$colorMatrix = [
0.6, 0.2, 0.2, 0.0, 0.0,
0.0, 0.0, 0.0, 0.0, 0.0,
0.0, 0.0, 0.0, 0.0, 0.0,
0.0, 0.0, 0.0, 1.0, 0.0,
0.0, 0.0, 0.0, 0.0, 0.0,
];
// The intensity of the red channel after applying this color matrix takes
// the values from the pixels before the transformation of:
// 60% of the red, 20% blue, 20% green
$imagick->colorMatrixImage($colorMatrix);
$imagick->separateImageChannel(\Imagick::CHANNEL_RED);
$imagick->thresholdImage(0.5 * \Imagick::getQuantum());
forceBlackAndWhite($imagick);
$imagick->setImageFormat('gif');
$imagick->writeImage("./outputColorMatrixChannelThreshold.gif");
}
colorMatrixChannelThreshold输出
上述代码的输出图像是: