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

时间:2021-12-24 00:20:59

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?

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

7 个解决方案

#1


10  

Simply round the grayscale color to either black or white.

只需将灰度颜色环绕为黑色或白色即可。

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

#2


19  

Using the php gd library:

使用php gd库:

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

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

有关更多示例,请查看上面链接中的用户注释。

#3


6  

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。您想用什么函数来决定像素应该是黑色还是白色?

#4


2  

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

如果您打算自己这样做,则需要实现抖动算法。但正如@jonni所说,使用现有工具会容易得多吗?

#5


1  

$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;}

#6


1  

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);

}

#7


0  

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中执行此操作将非常缓慢 - 您将更进一步找到提供此功能的库。

#1


10  

Simply round the grayscale color to either black or white.

只需将灰度颜色环绕为黑色或白色即可。

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

#2


19  

Using the php gd library:

使用php gd库:

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

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

有关更多示例,请查看上面链接中的用户注释。

#3


6  

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。您想用什么函数来决定像素应该是黑色还是白色?

#4


2  

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

如果您打算自己这样做,则需要实现抖动算法。但正如@jonni所说,使用现有工具会容易得多吗?

#5


1  

$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;}

#6


1  

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);

}

#7


0  

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中执行此操作将非常缓慢 - 您将更进一步找到提供此功能的库。