检测图片的“整体平均”颜色

我有一个JPG图像。

我需要知道“整体平均”图像的颜色。 乍一看,可以使用图像的直方图(通道RGB)。

在工作中,我主要使用JavaScript和PHP(一个小Python),因此欢迎这些语言的决定。 也许是图书馆工作与图像,解决类似的问题。

我不需要dynamic确定图片的颜色。 我只需要经过整个图像arrays,并分别确定每个图像的颜色(这些信息我将记住以备将来使用)。

您可以使用PHP来获取调色板的数组,如下所示:

 <?php function colorPalette($imageFile, $numColors, $granularity = 5) { $granularity = max(1, abs((int)$granularity)); $colors = array(); $size = @getimagesize($imageFile); if($size === false) { user_error("Unable to get image size data"); return false; } $img = @imagecreatefromjpeg($imageFile); // Andres mentioned in the comments the above line only loads jpegs, // and suggests that to load any file type you can use this: // $img = @imagecreatefromstring(file_get_contents($imageFile)); if(!$img) { user_error("Unable to open image file"); return false; } for($x = 0; $x < $size[0]; $x += $granularity) { for($y = 0; $y < $size[1]; $y += $granularity) { $thisColor = imagecolorat($img, $x, $y); $rgb = imagecolorsforindex($img, $thisColor); $red = round(round(($rgb['red'] / 0x33)) * 0x33); $green = round(round(($rgb['green'] / 0x33)) * 0x33); $blue = round(round(($rgb['blue'] / 0x33)) * 0x33); $thisRGB = sprintf('%02X%02X%02X', $red, $green, $blue); if(array_key_exists($thisRGB, $colors)) { $colors[$thisRGB]++; } else { $colors[$thisRGB] = 1; } } } arsort($colors); return array_slice(array_keys($colors), 0, $numColors); } // sample usage: $palette = colorPalette('rmnp8.jpg', 10, 4); echo "<table>\n"; foreach($palette as $color) { echo "<tr><td style='background-color:#$color;width:2em;'>&nbsp;</td><td>#$color</td></tr>\n"; } echo "</table>\n"; 

哪个给你一个数组,其值越高使用该颜色的频率越高。

编辑一个评论者问如何使用这个目录中的所有文件,这里是:

  if ($handle = opendir('./path/to/images')) { while (false !== ($file = readdir($handle))) { $palette = colorPalette($file, 10, 4); echo "<table>\n"; foreach($palette as $color) { echo "<tr><td style='background-color:#$color;width:2em;'>&nbsp;</td><td>#$color</td></tr>\n"; } echo "</table>\n"; } closedir($handle); } 

可能不想在太多的文件上这样做,但它是你的服务器。

另外,如果你想使用JavaScript Lokesh的Color-Theif库完全符合你的要求。

结合JKirchartz和亚历山大Hugestrand答案:

  function getAverage($sourceURL){ $image = imagecreatefromjpeg($sourceURL); $scaled = imagescale($image, 1, 1, IMG_BICUBIC); $index = imagecolorat($scaled, 0, 0); $rgb = imagecolorsforindex($scaled, $index); $red = round(round(($rgb['red'] / 0x33)) * 0x33); $green = round(round(($rgb['green'] / 0x33)) * 0x33); $blue = round(round(($rgb['blue'] / 0x33)) * 0x33); return sprintf('#%02X%02X%02X', $red, $green, $blue); } 

经过testing,返回hexstring。

从PIL开始。 http://www.pythonware.com/products/pil/

打开图像对象。 使用getdata方法获取所有像素。 平均值你回来。

像这样的东西。

使用python进行图像颜色检测

真彩色图像的较短解决scheme是将其缩小到1×1像素大小,并在该像素处对颜色进行采样:

$ scaled = imagescale($ img,1,1,IMG_BICUBIC); $ meanColor = imagecolorat($ img,0,0);

…但是我自己没有testing过

我已经创build了composer软件包,该软件包提供了从path中获取给定图像的平均颜色的库。

您可以通过在您的项目目录中运行以下命令来安装它:

 composer require tooleks/php-avg-color-picker 

用法示例:

 <?php use Tooleks\Php\AvgColorPicker\Gd\AvgColorPicker; $imageAvgHexColor = (new AvgColorPicker)->getImageAvgHexByPath('/absolute/path/to/the/image.(jpg|jpeg|png|gif)'); // The `$imageAvgHexColor` variable contains the average color of the given image in HEX format (#fffff). 

请参阅文档 。