Dominant color & PIL

Terry Hancock hancock at anansispaceworks.com
Mon Jan 23 10:12:06 EST 2006


On 23 Jan 2006 05:47:58 -0800
"Rinzwind" <w.damen at gmail.com> wrote:
> For example:
> 
> getdata
> 
> im.getdata() => sequence
> 
> Returns the contents of an image as a sequence object
> containing pixel values. The sequence object is flattened,
> so that values for line one follow directly after the
> values of line zero, and so on.
> 
> Note that the sequence object returned by this method is
> an internal PIL data type, which only supports certain
> sequence operations. To convert it to an ordinary sequence
> (e.g. for printing), use list(im.getdata()).
> 
> So you could get them and count them in :)
> Don't know if you should do that on a 1600x1200 wallpaper
> tho :D

I think he's thinking of how to get the values for the
master image.

> Terry Hancock wrote:
> > On Sun, 22 Jan 2006 21:07:45 +0100
> > Sebastjan Trepca <trepca at gmail.com> wrote:
> > > I was wondering is it possible to find out which
> > > colour is dominant in an image using PIL?
> > > It would be very easy to create interesting mozaic
> > > images with that :)
> >
> > Shrink it to one pixel, and get that pixel's value. ;-)
> >
> > Seriously, that ought to do it.  Bear in mind that you
> > need to use the right sampling mode (IIRC, you want
> > ANTIALIAS).

This is answering the OP's question which is how to get
the average color for a whole image:

color = img.resize( (1,1), Image.ANTIALIAS).getpixel((0,0))

You just shrink the image down. The ANTIALIAS filter
averages every pixel in the image (this occurs in the PIL
library, so it's probably pretty fast), and then you fish
the single remaining pixel out into tuple format.

-- 
Terry Hancock (hancock at AnansiSpaceworks.com)
Anansi Spaceworks http://www.AnansiSpaceworks.com




More information about the Python-list mailing list