python - Sum of colorvalues of an image -


i looking way sum color values of pixels of image. require estimate total flux of bright source (say distant galaxy) surface brightness image. please me how can sum colour values of pixels of image.

for example: each pixel of following image has colour value in between 0 1. when read image imread colour values of each pixel array of 3 elements. new in matplotlib , not know how can convert array single values in scale of 0 1 , add them.

enter image description here

if have pil image, can convert greyscale ("luminosity") this:

from pil import image col = image.open('sample.jpg') gry = col.convert('l') # returns grayscale version. 

if want ot have more control on how colors added, convert numpy array first:

arr = np.asarray(col) tot = arr.sum(-1)  # sum on color (last) axis mn  = arr.mean(-1) # or mean, keep same normalization (0-1) 

or can weight colors differently:

wts = [.25, .25, .5]    # in order: r, g, b tot = (arr*wts).sum(-1) # blue has twice weight of red , green 

for large arrays, equivalent last line , faster, possibly harder read:

tot = np.einsum('ijk, k -> ij', arr, wts) 

all of above adds colors of each pixel, turn color image grayscale (luminosity) image. following add pixels see integral of entire image:

tot = arr.sum(0).sum(0) # first sums rows, second sums columns 

if have color image, tot still have 3 values. if image grayscale, single value. if want mean value, replace sum mean:

mn = arr.mean(0).mean(0) 

Comments

Popular posts from this blog

PHPMotion implementation - URL based videos (Hosted on separate location) -

javascript - Using Windows Media Player as video fallback for video tag -

c# - Unity IoC Lifetime per HttpRequest for UserStore -