检测图片的“总体平均”颜色
我有一张jpg格式的图片。
我想知道这张图片的“整体平均”颜色。乍一看,可以使用图片的直方图(RGB通道)来实现这个目的。
在工作中,我主要使用JavaScript和PHP(还有一点Python),所以如果能用这些语言来解决问题就最好了。也许有一些库可以帮助处理图片,解决类似的问题。
我并不需要动态地确定图片的颜色。我只需要一次性遍历整个图片数组,分别确定每张图片的颜色(这些信息我会记下来,以备将来使用)。
7 个回答
3
$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和$b来获取平均颜色的值。对图像进行重采样比仅仅缩放它要好得多!
7
结合了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);
}
经过尝试和测试,返回的是十六进制字符串。
76
你可以用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;'> </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;'> </td><td>#$color</td></tr>\n";
}
echo "</table>\n";
}
closedir($handle);
}
不过最好不要在太多文件上这样做,但这毕竟是你的服务器。
另外,如果你更喜欢用Javascript,可以试试Lokesh的Color-Thief库,它正好能满足你的需求。