[Numpy-discussion] setting rows of a numpy array

Christopher Barker Chris.Barker@noaa....
Mon Apr 28 14:10:35 CDT 2008


A couple suggestions:

wilson wrote:
> I am setting
> the each row using pixel values of each image in some folder.

As an image is generally a 2-d array of pixels, you may want to use a N 
X Width X Height 3-d array, rather than flattening each image to a 
single row.

> def putrow(myarray,inrow ,rownum):
>     myarray[rownum,:]=inrow

I'm not sure I'd both writing a function for a one liner like that -- 
why not just put it in the code and avoid the function overhead?

> for i in range(numofimgs):
>      pixarray=asfarray(myimagefileslist[i]._pixelarray)
>      putrow(myimagesdata,pixarray,i)

no need to make an array out of _pixelarray first -- you can just do:

for i in range(numofimgs):
      myimagesdata[i,:] = myimagefileslist[i]._pixelarray

Can you load the data right into myimagesdata up front, rather than 
creating your MyImage instances first? That'll save you a bunch of data 
copying. In fact, if you build the big array, you can then have the 
MyImage objects reference a view intot he big array, so no data copying:

myimagefileslist[i]._pixelarray = myimagesdata[i,:]

will give you a 1-d array that shares data with the main 2-d array.

-Chris


-- 
Christopher Barker, Ph.D.
Oceanographer

Emergency Response Division
NOAA/NOS/OR&R            (206) 526-6959   voice
7600 Sand Point Way NE   (206) 526-6329   fax
Seattle, WA  98115       (206) 526-6317   main reception

Chris.Barker@noaa.gov


More information about the Numpy-discussion mailing list