average of PIL images

Robert Kern robert.kern at gmail.com
Mon Feb 18 15:38:23 EST 2008


vaneric wrote:
> hi
> i have a set of RGB images of diff faces (of people )as a 2 dim
> numpyarray
> ..something like
> threefaces=array([[xa1,xa2,xa3],
>        [xb1,xb2,xb3],
>        [xc1,xc2,xc3]])
> where xa1,xa2,xa3 are  tuples each representing rgb values of a pixel
> of first image ..
> 
> i need to create the average face image and display it.problem is i
> need to calculate (xa1+xb1+xc1)/3  etc to calculate avearge value of
> each pixel.how can i do this calculation.

threefaces.mean(axis=0)

> do i need to convert the
> r,g,b in to a single value for each pixel?

It depends on the problem that you are trying to solve. If monochrome images are
acceptable for your problem, then it is probably best to convert all your images 
to monochrome to do the averaging. At least for a first cut. Averaging color 
images is tricky; you really shouldn't do it in the RGB colorspace. There are a 
couple of colorspaces which you could choose; different problems require 
different colorspaces.

> the average value of a
> pixel will be a float isn't it?

Yes.

> how can i create a PIL image from
> this?

# Cast the floating point data to bytes.
avgface = avgface.astype(numpy.uint8)

# Create the PIL image from the numpy data.
img = Image.fromstring('L', (width, height), avgface.tostring())

-- 
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless enigma
  that is made terrible by our own mad attempt to interpret it as though it had
  an underlying truth."
   -- Umberto Eco




More information about the Python-list mailing list