What is an efficient way to read data from an image with an arbitrary number of pixel components? (Python, PIL) -


i working on project in python requires me able pixel values across entire image, in order weigh similarity (it image segmentation project). using pil image module (and every other package in anaconda).

much of our algorithim developed, though returning because of right can handle grayscale images. done so:

self.dataimage = image.open(self.file, mode = 'r') self.data = self.dataimage.getdata() 

i later inspect with

print("data: {}".format(list(data.getimagedata()))) 

this returns sequence of integers corresponding intensity of each pixel of image. wanted make simple modification allow if accept rgb images also, making each item in sequence array instead of single integer, able support images arbitrary number of pixel components (such 3, rgb).

is there simple way through pil image module? looked through documentation , here on stackoverflow, , best find:

self.allpixels = self.dataimage.load()     self.data = []     in range(self.width):         j in range(self.height):             currentpixel = self.allpixels[i,j]             self.data.append(currentpixel)  print("data: {}".format(data.getimagedata())) 

this supposed return data in form of (255,255,255) (for example) each pixel. should output of when looking @ grayscale image? because output of first method described, , second, comepletely different.

the first (using getdata()) gives

data: [0, 0, 4, 4, 3, 3, 2, 0, 3, 0, 0, 2, 5, 1, 0, 0, 5, 6, 6, 2, 0, 0, 2, 7, 8 .... 

and second (using load() , loop above) gives

data: [0, 4, 1, 0, 12, 9, 0, 4, 0, 12, 0, 19, 123, 167, 149, 156, 159, 158, 170, 113 .... 

is giving me averages of rgb components? how determine if there no rgb components , grayscale image?

besides that, there better way should this? ideally want have same output getdata() grayscale image, can support arbitrary number of pixel components. know looping through each , every pixel , entering them each individual arrays, become time , memory consuming large image, no?

any advice appreciated. if wondering, reason need support high number of pixel components because being written application on data gathered x-ray microscope @ our laboratory. while rgb has 3 channels, may have on 20 channels in single image, each 1 corresponding elemental composition rather color (iron, potassium, zinc, calcium, etc.).

numpy (part of anaconda) can load pixel data pil image using asarray

in [1]: pil import image in [2]: import numpy np in [3]: x = image.open("/tmp/kitten.jpg") in [4]: np.asarray(x) out[4]: array([[[  1,   2,  23],     ..., dtype=uint8) 

you'll 3 dimensional array, first 2 dimensions being position of pixel in image, , third dimension being value of pixel in image in whatever color format picture in. has advantage of being faster getdata.


Comments

Popular posts from this blog

c - Bitwise operation with (signed) enum value -

xslt - Unnest parent nodes by child node -

YouTubePlayerFragment cannot be cast to android.support.v4.app.Fragment -