Colour sampling

Dave Angel davea at
Fri Oct 16 05:15:02 CEST 2009

Anthra Norell wrote:
> <div class="moz-text-flowed" style="font-family: -moz-fixed">Dylan 
> Palmboom wrote:
>> Does anyone know what python libraries are available to do the 
>> following:
>> 1. I would like to take a photograph of an object with a colour. In this
>> case, it is a sheet of sponge.
>> 2. Feed this image into a function in a python library and let the 
>> function
>> "automatically scan" this image's
>>     pixel colours, and return an average of all the pixels' colours 
>> in the
>> image.
>> 3. I could then use the rgb values to set a colour swatch for the 
>> object's
>> colour.
>> Please let me know if you have any ideas. It would be appreciated.
>> In the meantime, I will search Google and see what I can find.
>> Thanks
> image.histogram () returns a list of pixel counts: red: index 0 - 255, 
> green: 256 - 511, blue: 512 - 767. Figure out the median for each 
> color and there you go.
>   This would obviously not work well with pictures containing a wide 
> range of colors, such as a landscape, but then color-averaging a 
> colorful picture wouldn't make much sense anyway. You seem to be 
> working with uniformly colored objects for which the method should 
> work well, as you'll get a narrow range of values with high counts in 
> each color band.
> Frederic
> </div>
You should look at PIL, the Python Imaging Library.  Or if you're 
writing a GUI program, there's undoubtedly a jpeg decoder built into 
whatever library you're using.  For example, if you're using wxPython, 
you might want to look at wx.Image, and wx.JPEGHandler.

PIL --

wxPython  --


More information about the Python-list mailing list