你如何在 PHP 中将图像转换为黑白

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/254388/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 22:05:01  来源:igfitidea点击:

How do you convert an image to black and white in PHP

phpimageimagefilter

提问by AnnanFay

How does one go about converting an image to black and white in PHP?

如何在 PHP 中将图像转换为黑白?

Not just turning it into greyscale but every pixel made black or white?

不只是把它变成灰度,而是每个像素都变成黑色或白色?

采纳答案by Jasper Bekkers

Simply round the grayscale color to either black or white.

只需将灰度颜色四舍五入为黑色或白色。

float gray = (r + g + b) / 3
if(gray > 0x7F) return 0xFF;
return 0x00;

回答by Joel Wietelmann

Using the php gdlibrary:

使用 php gd库:

imagefilter($im, IMG_FILTER_GRAYSCALE);
imagefilter($im, IMG_FILTER_CONTRAST, -100);

Check the user comments in the link above for more examples.

查看上面链接中的用户评论以获取更多示例。

回答by jonnii

You could shell out to imagemagick, assuming your host supports it. What function do you want to use for deciding if a pixel should be black or white?

假设您的主机支持它,您可以使用 imagemagick。你想用什么函数来决定一个像素应该是黑色还是白色?

回答by toolkit

If you intend to do this yourself, you will need to implement a dithering algorithm. But as @jonnisays, using an existing tool would be much easier?

如果您打算自己执行此操作,则需要实现抖动算法。但正如@jonni所说,使用现有工具会容易得多?

回答by KEL

$rgb = imagecolorat($original, $x, $y);
        $r = ($rgb >> 16) & 0xFF;
        $g = ($rgb >> 8 ) & 0xFF;
        $b = $rgb & 0xFF;

        $gray = $r + $g + $b/3;
        if ($gray >0xFF) {$grey = 0xFFFFFF;}
        else { $grey=0x000000;}

回答by Amed

This function work like a charm

这个功能就像一个魅力

    public function ImageToBlackAndWhite($im) {

    for ($x = imagesx($im); $x--;) {
        for ($y = imagesy($im); $y--;) {
            $rgb = imagecolorat($im, $x, $y);
            $r = ($rgb >> 16) & 0xFF;
            $g = ($rgb >> 8 ) & 0xFF;
            $b = $rgb & 0xFF;
            $gray = ($r + $g + $b) / 3;
            if ($gray < 0xFF) {

                imagesetpixel($im, $x, $y, 0xFFFFFF);
            }else
                imagesetpixel($im, $x, $y, 0x000000);
        }
    }

    imagefilter($im, IMG_FILTER_NEGATE);

}

回答by Hugh Bothwell

For each pixel you must convert from color to greyscale - something like $grey = $red * 0.299 + $green * 0.587 + $blue * 0.114; (these are NTSC weighting factors; other similar weightings exist. This mimics the eye's varying responsiveness to different colors).

对于每个像素,您必须从颜色转换为灰度 - 类似于 $grey = $red * 0.299 + $green * 0.587 + $blue * 0.114;(这些是 NTSC 权重因子;存在其他类似的权重。这模仿了眼睛对不同颜色的不同反应)。

Then you need to decide on a cut-off value - generally half the maximum pixel value, but depending on the image you may prefer a higher value (make the image darker) or lower (make the image brighter).

然后您需要决定一个截止值 - 通常是最大像素值的一半,但根据图像,您可能更喜欢更高的值(使图像更暗)或更低(使图像更亮)。

Just comparing each pixel to the cut-off loses a lot of detail - ie large dark areas go completely black - so to retain more information, you can dither. Basically, start at the top left of the image: for each pixel add the error (the difference between the original value and final assigned value) for the pixels to the left and above before comparing to the cut-off value.

只是将每个像素与截止值进行比较会丢失很多细节 - 即大的黑暗区域变成完全黑色 - 因此为了保留更多信息,您可以抖动。基本上,从图像的左上角开始:对于每个像素,在与截止值进行比较之前,将左侧和上方像素的误差(原始值和最终指定值之间的差异)相加。

Be aware that doing this in PHP will be very slow - you would be much further ahead to find a library which provides this.

请注意,在 PHP 中执行此操作会非常慢 - 您将更早地找到提供此功能的库。