O'Reilly logo

Flickr Hacks by Jim Bumgardner, Paul Bausch

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Hack #44. Find the Dominant Color of an Image

Flickr doesn't have a color search feature, but that shouldn't stop you from building one!

There are a lot of cool things you can do if you know the dominant colors of a group of images. For instance, you can make user interfaces for searching large quantities of photos by color, such as the Flickr Colr Pickr [Hack #45] , and you can create fascinating collages by arranging the photos by luminance, saturation, or hue.

The simplest way I've found to find the dominant color of an image is to resize the image to 1 x 1 (producing a single pixel) and then record the color of that pixel. This will produce a value that is (or is close to, depending on the resizing algorithm) the average color of the image.

This hack provides two scripts: samplePhoto.pl samples a single photo, and sampleSnaps.pl samples a large collection of photos (thumbnails, hopefully) and saves the information in a file that can be read by other scripts.

What You Need

To run the scripts in this hack, you'll need the following two modules:

Image::Magick

This Perl module provides an API for the powerful ImageMagick image-processing library. You can find it at http://www.imagemagick.org, or you can download it from CPAN.

Tip

For Windows, the Perl module comes with the latest installer from the ImageMagick site; however, you might have problems getting this version to install properly, depending on your Perl configuration. If, after installing ImageMagick, you have problems with the ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required