检测图片的"整体平均"颜色

Kal*_*nin 46 javascript php python image-processing

我有一个jpg图像.

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

在工作中我主要使用JavaScript和PHP(一点点Python)因此欢迎这些语言的决定.也许是用于处理解决类似问题的图像的库.

我不需要动态确定图片的颜色.我只需要浏览整个图像阵列并分别确定每种图像的颜色(这些信息我将记住以备将来使用).

JKi*_*rtz 71

您可以使用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";
Run Code Online (Sandbox Code Playgroud)

这为您提供了一个数组,其值越大,使用颜色的频率就越高.

编辑 评论者询问如何在目录中的所有文件上使用它,这里是:

    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);
    }
Run Code Online (Sandbox Code Playgroud)

可能不想在太多文件上执行此操作,但它是您的服务器.

或者,如果您更喜欢使用Javascript Lokesh的Color-Theif库,那么您正在寻找它.


Joh*_*kex 5

结合JKirchartz和Alexander 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); 
 }
Run Code Online (Sandbox Code Playgroud)

经过测试,返回十六进制字符串。

  • 第3行不应该引用$ scaled而不是$ image吗?这似乎是在创建缩放后的图像,但从未使用过-而是将结果基于原始图像的0,0像素处。如果我的阅读是正确的,您可能想更新答案。 (2认同)