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

时间:2010-08-12 14:02:53

标签: php javascript python image-processing

我有一张jpg图片。

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

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

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

7 个答案:

答案 0 :(得分:70)

您可以使用 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's Color-Theif库,那么您正在寻找它。

答案 1 :(得分:4)

结合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); 
 }

尝试并测试,返回十六进制字符串。

答案 2 :(得分:2)

真彩色图像的一个较短的解决方案是将其缩小到1x1像素大小并在该像素处采样颜色:

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

...但我自己没有测试过。

答案 3 :(得分:2)

$img = glob('img/*');
foreach ($img as $key => $value) {
    $info = getimagesize($value);
    $mime = $info['mime'];
    switch ($mime) {
        case 'image/jpeg':
            $image_create_func = 'imagecreatefromjpeg';
            break;
        case 'image/png':
            $image_create_func = 'imagecreatefrompng';
            break;
        case 'image/gif':
            $image_create_func = 'imagecreatefromgif';
            break;
    }
    $avg = $image_create_func($value);
    list($width, $height) = getimagesize($value);
    $tmp = imagecreatetruecolor(1, 1);
    imagecopyresampled($tmp, $avg, 0, 0, 0, 0, 1, 1, $width, $height);
    $rgb = imagecolorat($tmp, 0, 0);
    $r = ($rgb >> 16) & 0xFF;
    $g = ($rgb >> 8) & 0xFF;
    $b = $rgb & 0xFF;
    echo '<div style="text-align:center; vertical-align: top; display:inline-block; width:100px; height:150px; margin:5px; padding:5px; background-color:rgb('.$r.','.$g.','.$b.');">';
    echo '<img style="width:auto; max-height:100%; max-width: 100%; vertical-align:middle; height:auto; margin-bottom:5px;" src="'.$value.'">';
    echo '</div>';

你可以用$ r,$ g和&amp;获得平均颜色的值。 $ b 重新取样图像比仅缩放图像要好得多!

答案 4 :(得分:1)

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

打开Image对象。使用getdata方法获取所有像素。平均你得到的值。

像这样。

Image color detection using python

答案 5 :(得分:0)

我创建了一个编辑器包,它提供了一个库,用于通过路径从给定图像中选取平均颜色。

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

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).

请参阅documentation

答案 6 :(得分:0)

这是使用php-vips的解决方案。它的速度非常快,可以找到最多的 common 颜色,而不是平均颜色。

大多数照片的平均灰度为灰色,因为这就是自动白平衡的功能。您真正想要的是最常出现的颜色。

该程序使用3D直方图。它制作了一个10 x 10 x 10的立方体(您可以更改此形状,请参见$n_bins)以表示整个RGB色彩空间,然后循环遍历图像,计算落入每个bin中的像素数。它将bin(0,0,0)中的计数设置为零(黑色通常是不感兴趣的背景),然后搜索计数最高的bin。该bin的索引是最常见的RGB颜色。

这不适用于大多数PNG(您需要弄平Alpha)或CMYK(需要先转换为RGB)。

#!/usr/bin/env php
<?php

require __DIR__ . '/vendor/autoload.php';
use Jcupitt\Vips;

$im = Vips\Image::newFromFile($argv[1], ['access' => 'sequential']);

# 3D histogram ... make 10 x 10 x 10 bins, so 1000 possible colours
$n_bins = 10;
$hist = $im->hist_find_ndim(['bins' => $n_bins]);

# black is usually background or boring, so set that cell to 0 counts
# fetch (0, 0, set the 0th element of that to 0, paste back
$pixel = $hist->getpoint(0, 0);
$pixel[0] = 0;
$pixel = Vips\Image::black(1, 1)->add($pixel);
$hist = $hist->insert($pixel, 0, 0);

# (x, y) pixel with the most counts
[$v, $x, $y] = $hist->maxpos();
$pixel = $hist->getpoint($x, $y);
$z = array_search($v, $pixel);

# convert indexes to rgb ... +0.5 to get the centre of each bin
$r = ($x + 0.5) * 256 / $n_bins;
$g = ($y + 0.5) * 256 / $n_bins;
$b = ($z + 0.5) * 256 / $n_bins;

echo("r = " . $r . "\n");
echo("g = " . $g . "\n");
echo("b = " . $b . "\n");

我可以这样运行它:

$ time ./try302.php ~/pics/shark.jpg 
r = 38.4
g = 38.4
b = 12.8
real    0m0.077s
user    0m0.068s
sys 0m0.016s

这台普通笔记本电脑上的时间为70毫秒,可生成700 x 700像素的jpg。