PHP:获取一个像素(图像)的灰度值
PHP: get gray level of a pixel (image)
我想获得 PHP 中给定图像的精确 "gray level"。我不明白为什么我总是使用 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
图像中的第一个像素位于 (0, 0),而不是 (1, 1)。尝试改变
$gray = imagecolorat($grayLevel, 1, 1);
至
$gray = imagecolorat($grayLevel, 0, 0);
我想获得 PHP 中给定图像的精确 "gray level"。我不明白为什么我总是使用 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
图像中的第一个像素位于 (0, 0),而不是 (1, 1)。尝试改变
$gray = imagecolorat($grayLevel, 1, 1);
至
$gray = imagecolorat($grayLevel, 0, 0);