【问题标题】:Setting the grayscale value of a pixel in php在php中设置像素的灰度值
【发布时间】:2011-06-24 10:22:16
【问题描述】:

我有一个 jpeg 图像资源加载到 php 中的一个变量中。给定一个灰度值,例如 6,如何将单个特定像素设置为该灰度值?我的客户已经非常清楚地表明灰度和 rgb 之间存在很大差异。

这在 php 中是否可以使用 GD 库?如果有,怎么做?

注意:该脚本确实对整个图像进行了灰度化,但使用了一种晦涩的算法。我的脚本获取每个像素的RGB,并获取算法对应的灰度值。它现在只需要将像素转换为特定的灰度值。

【问题讨论】:

    标签: php grayscale


    【解决方案1】:

    您可能会发现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);
        }
    }
    

    【讨论】:

      【解决方案2】:

      您是否可以访问终端或类似的东西来安装 Image Magick。如果是这样,那么您可以使用以下

      convert image.jpg -colorspace Gray image-bw.jpg
      

      抱歉,我不确定如何使用 PHP GD 库。

      【讨论】:

      • 你没看我写的,这不是标准的灰度算法
      猜你喜欢
      • 2012-06-01
      • 1970-01-01
      • 2017-11-17
      • 2013-07-28
      • 2015-06-08
      • 2020-05-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多