【问题标题】:PHP: get gray level of a pixel (image)PHP:获取像素(图像)的灰度
【发布时间】:2017-11-17 06:12:46
【问题描述】:

我想在 PHP 中获得给定图像的确切“灰度”。我不明白为什么我总是使用imagecolorsforindex[0, 0, 0] 作为RGB 数组。这是我的代码:

// load
$img = imagecreatefromjpeg("images/white.jpeg");

// to grayscale
imagefilter($img, IMG_FILTER_GRAYSCALE);

// just 1 pixel that describe the whole image
$grayLevel = resize_image($img, 1, 1);
$gray = imagecolorat($grayLevel, 1, 1);
$readble = imagecolorsforindex($grayLevel, $gray);

print_r($readble);

我首先加载我的图像(这是一张全白图像,因此根据this,每个像素都应该是 [255, 255, 255]),然后将其转换为灰度。我将图像大小调整为仅描述整个图像的一个像素,然后得到 RGB 数组。数组是[0, 0, 0]

我不明白为什么我总是收到带有灰度和彩色图像的[0, 0, 0]。 1 像素技术错了吗?

非常感谢

PS:resize_image 有效,我已经测试过了,无论如何这是代码:https://pastebin.com/HNDzKq5E

【问题讨论】:

    标签: php image grayscale


    【解决方案1】:

    图像中的第一个像素存在于 (0, 0),而不是 (1, 1)。尝试改变

    $gray = imagecolorat($grayLevel, 1, 1);

    $gray = imagecolorat($grayLevel, 0, 0);

    【讨论】:

    • 你是对的!我对使用 1,1 的调整大小调用感到困惑。 :)
    猜你喜欢
    • 1970-01-01
    • 2023-03-07
    • 1970-01-01
    • 1970-01-01
    • 2015-08-31
    • 2015-06-08
    • 2012-04-24
    • 1970-01-01
    • 2013-09-16
    相关资源
    最近更新 更多