average of PIL images

7stud bbxx789_05ss at yahoo.com
Mon Feb 18 16:05:44 EST 2008


On Feb 18, 1:58 pm, 7stud <bbxx789_0... at yahoo.com> wrote:
> On Feb 18, 10:18 am, vaneric <vaneric... at gmail.com> 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.do i need to convert the
> > r,g,b in to a single value for each pixel? the average value of a
> > pixel will be a float isn't it? how can i create a PIL image from
> > this?
> > any help,directive greatly appreciated
> > eric
>
> import Numeric
>
> arr = Numeric.array([
>     [1, 2, 3],
>     [4, 5, 6],
>     [7, 8, 9],
>     [10, 11, 12]
> ])
>
> col1 = arr[0:,0]
> print col1
>
> sum = 0
> count = 0
> for num in col1:
>     sum += num
>     count += 1
>
> avg = (sum * 1.0)/count   #convert one term to a float to get float
> result
> print avg
> print round(avg)
> print int(round(avg))
>
> print arr[0]
>
> size = len(arr[0])
> for i in range(size):
>     col = arr[0:, i]
>     sum = 0
>     count = 0
>
>     for num in col:
>         sum += num
>         count += 1
>
>     result = (sum * 1.0) /count
>     print result,
>     result = int(round(result))
>     print result
>
> --output:--
> [ 1  4  7 10]
> 5.5
> 6.0
> 6
> [1 2 3]
> 5.5 6
> 6.5 7
> 7.5 8

In this statement:

> col1 = arr[0:,0]

the first term is the row or row range, and the second term is the
column or column range.  If you write this:

num = arr[0,0]

you get the element in row 0, column 0.  But you can also specify
ranges for each col or row:

num = arr[1:, 2:]

That says to get all elements from row 1 to the bottom that are in
from column 2 to the end of the row.



More information about the Python-list mailing list