Image Average Color Solutions

You have a batch of images that you want to grab an average color from each one so you can make "small" colored thumbnails that represent the images, or maybe you actually need the hex color from each one for some reason.

This can be done very easily using php and Imagemagick. Below is a code snippet that was used to convert over 50,000 images to hex colors for a recent project.



  $image = new Imagick('yourimage.jpg');

  $image->scaleImage(1, 1, true);

  $pixel = $image->getImagePixelColor(0,0);

  $red = ($rgb >> 16) & 0xFF;

  $green = ($rgb >> 8) & 0xFF;

  $blue = $rgb & 0xFF;



Obviously there was some logic wrapping this looping through all the images and storing the resulting hex colors into a database. Your application of this method will probably be different so that was not included.

This code is very fast because all it does is scale the image down to a single pixel and sample it. The default logic in scaling will take an average sample of the entire picture, and store it in that 1 pixel.