【问题标题】:how to count number of pixels in image (php)如何计算图像中的像素数(php)
【发布时间】:2012-10-28 23:12:50
【问题描述】:

请帮我计算图像中的像素数,或者输出 RGB 数组。

所以这是从数组中给我一个元素的脚本:

<?php
    $img = "1.png";
    $imgHand = imagecreatefrompng("$img");
    $imgSize = GetImageSize($img);
    $imgWidth = $imgSize[0];
    $imgHeight = $imgSize[1];
    echo '<img src="'.$img.'"><br><br>';
    for ($l = 0; $l < $imgHeight; $l++) {
        for ($c = 0; $c < $imgWidth; $c++) {
            $pxlCor = ImageColorAt($imgHand,$c,$l);
            $pxlCorArr = ImageColorsForIndex($imgHand, $pxlCor);
        }
    }


        print_r($pxlCorArr); 
?>

对不起我的英语来自乌克兰

【问题讨论】:

  • $numOfPix = $imgWidth * $imgHeight;
  • 你到底想做什么?图像中的像素总数,如 imgwidth x imageheight 还是什么?
  • 我相信他想要整个图像的$pxlCorArr,其中索引是像素,值是颜色..否则他的代码没有任何意义。
  • 我尝试将所有像素放入数组中,然后输出 RGB 代码

标签: php


【解决方案1】:

图像中的像素数只是高度乘以宽度。

但是,我认为这就是你想要的:

<?php
    $img = "1.png";
    $imgHand = imagecreatefrompng("$img");
    $imgSize = GetImageSize($img);
    $imgWidth = $imgSize[0];
    $imgHeight = $imgSize[1];
    echo '<img src="'.$img.'"><br><br>';

    // Define a new array to store the info
    $pxlCorArr= array();

    for ($l = 0; $l < $imgHeight; $l++) {
        // Start a new "row" in the array for each row of the image.
        $pxlCorArr[$l] = array();

        for ($c = 0; $c < $imgWidth; $c++) {
            $pxlCor = ImageColorAt($imgHand,$c,$l);

            // Put each pixel's info in the array
            $pxlCorArr[$l][$c] = ImageColorsForIndex($imgHand, $pxlCor);
        }
    }

    print_r($pxlCorArr); 
?>

这会将图像的所有像素数据存储在pxlCorpxlCorArr 数组中,然后您可以对其进行操作以输出您想要的内容。

该数组是一个二维数组,这意味着您可以使用从[0][0] 开始的$pxlCorArr[y][x] 引用单个像素。

【讨论】:

  • @VolodyaDaniliv 我更新了它以将数据存储在二维数组中,这样您就可以在数组中引用具有某种 x,y 坐标的单个像素。不确定这是否是您想要的,但现在您知道它是如何完成的,我相信您可以弄清楚。
猜你喜欢
  • 1970-01-01
  • 2021-02-23
  • 1970-01-01
  • 2012-06-07
  • 1970-01-01
  • 2022-01-07
  • 2023-03-26
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多