我有一个 jpeg 图像资源加载到 php 中的一个变量中。给定一个灰度值,例如 6,如何将单个特定像素设置为该灰度值?我的客户非常清楚灰度和 rgb 之间存在很大差异。
这在带有GD库的php中是否可行?如果是这样,怎么做?
注意:该脚本确实对整个图像进行灰度化,但使用了一种晦涩的算法。我的脚本获取每个像素的RGB,并获取算法对应的灰度值。它现在只需要将像素转换为特定的灰度值。
您可能会发现该imagesetpixel
功能很方便。以下是如何使用它的示例:
// $image = <GD image resource>
for($x = 0; $x < $width; x++)
{
for($y = 0; $y < $height; $y++)
{
$value = specialFunction($rgb_value);
// Depending on what the above function returns, the call below
// might have to be changed
$color = imagecolorallocate($image, $value, $value, $value);
imagesetpixel($image, $x, $y, $color);
}
}
您是否可以访问终端或类似的东西来安装 Image Magick。如果是这样,那么您可以使用以下内容
convert image.jpg -colorspace Gray image-bw.jpg
抱歉,我不确定如何使用 PHP GD 库来做到这一点。