I need to loop through the colors in a 16x16 px image file and find the most common/average color. Does anyone have any ideas as to the best way to do this?
Is there anything on the backend that I can use or should I do this on the front end with flash?
Thx!!
Aaron
Probably easiest to do on the server using something like ImageMagick/MagickWand - http://www.bitweaver.org/doc/magickwand/MagickGetImagePixels.html
- 20 November 2006 08:18 PM
-
here’s a quick one:
[php]
<?php
$box = imagecreatefromjpeg(‘test.jpg’);
$bxw = imagesx($box);
$bxh = imagesy($box);
$pixels = array();
for ($i = 0; $i < $bxh; $i++) {
for ($j = 0; $j < $bxw; $j++) {
$color = dechex( imagecolorat($box, $j, $i) );
$colorl = strlen($color);
if ($colorl < 6) {
$color = str_repeat(‘0’, 6 - $colorl) . $color;
}
if (array_key_exists($color, $pixels)) {
$pixels[$color]++;
} else {
$pixels[$color] = 1;
}
}
}
asort($pixels);
$common_color = end( array_keys($pixels) );
$color_count = array_pop($pixels);
echo “The common color is: #$common_color with $color_count count(s).”;
?>[/php]
my idea basically is, you record the values in an array, with the array key being the color itself, if the array key exists, then you just increment its value, if it’s not, then you add it to the array, then at the end, you just sort the array and get the end element.
- 20 November 2006 09:12 PM
-
Author
Hey, thanks guys!
jr_avilio, that’s exactly what I was thinking- the only missing piece to the puzzle is this- is there any way to break down the color count into say 64 colors or something.. I am concerned that if I allow the range of colors to get too great, there might be too much variation to get a strong reading..
Of course, on the other hand, I don’t want to go too far, as I will then have the opposite problem..
Thoughts?
- 20 November 2006 09:57 PM
-
Author
Alright,
I just started tinkering & this seems to work on some images and not so well on others. Anything stick out to you that I can optimize?
<?php
function ImageTrueColorToPalette2( $image, $dither, $ncolors )
{
$width = imagesx( $image );
$height = imagesy( $image );
$colors_handle = ImageCreateTrueColor( $width, $height );
ImageCopyMerge( $colors_handle, $image, 0, 0, 0, 0, $width, $height, 100 );
ImageTrueColorToPalette( $image, $dither, $ncolors );
ImageColorMatch( $colors_handle, $image );
ImageDestroy( $colors_handle );
}
$filepath = $_GET[ "file" ];
$sourceimage = imagecreatefromjpeg( $filepath );
$width = imagesx($sourceimage);
$height = imagesy($sourceimage);
$newwidth = 5;
$newheight = 5;
ImageTrueColorToPalette2( $sourceimage, FALSE, 5 );
$outputimage = imagecreatetruecolor($newwidth, $newheight);
imagecopyresampled($outputimage, $sourceimage, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
imagejpeg($outputimage,"temp.jpg",100);
$bxw = imagesx($outputimage);
$bxh = imagesy($outputimage);
$pixels = array();
for ($i = 0; $i < $bxh; $i++) {
for ($j = 0; $j < $bxw; $j++) {
$color = dechex( imagecolorat($outputimage, $j, $i) );
$colorl = strlen($color);
if ($colorl < 6) {
$color = str_repeat('0', 6 - $colorl) . $color;
}
if (array_key_exists($color, $pixels))
{
$pixels[$color]++;
}
else
{
$pixels[$color] = 1;
}
}
}
asort($pixels);
$common_color = end( array_keys($pixels) );
$color_count = array_pop($pixels);
?>
& Here are a couple previews:
http://lab-media.com//build/ttu/_tests/mostCommonColor.php?file=red.jpg
http://lab-media.com//build/ttu/_tests/mostCommonColor.php?file=_ADL6311_rt.jpg
http://lab-media.com//build/ttu/_tests/mostCommonColor.php?file=maskedriderstatue.jpg
http://lab-media.com//build/ttu/_tests/mostCommonColor.php?file=_ADL2820_rt.jpg
http://lab-media.com//build/ttu/_tests/mostCommonColor.php?file=colone.jpg
http://lab-media.com//build/ttu/_tests/mostCommonColor.php?file=DSC_7424_rt.jpg
http://lab-media.com//build/ttu/_tests/mostCommonColor.php?file=football2smallweb.jpg
Thx!!
Aaron
- 21 November 2006 07:01 AM
-
Author
I just found out from my client’s IT group that we cannot use php. Is it possible to convert this to ASP?
—edit—
I may end up running the admin & feeds on another server that can run php, so plz feel free to offer any ideas to fix the issues i mentioned previously & if any ahas happen re:porting to asp, I would love to hear em.
thanks again!!
Aaron
- 21 November 2006 03:04 PM
-
Of course, on the other hand, I don’t want to go too far, as I will then have the opposite problem..
what do you mean it doesn’t work so well on others? it could be that the conversion to palette leads to a significant color loss? like the red.jpg for example, too much colors were lost in the palette shift.
i dont know if asp can do it, asp.net probably can, any asp dudes around?
- 21 November 2006 07:03 PM
-
Author
http://lab-media.com//build/ttu/mostCommonColor.php?file=_ADL2820_rt.jpg seems to be pulling a color that doesnt seem to dominate the canvas, but it may just be my eyes..
- 21 November 2006 07:08 PM
-
Yo,
I got same Idea few months ago, but I had no time to do something similar.
Now I decided to do such a thingy in flash (frontend).
The rar includes swf & fla.
If someone wants to improve the script, please contact me, or much better: post your improvements here, thx
Grab it here
greetings, dan
- 22 November 2006 12:27 AM
-
- Log in or join for free to make a comment.


