Hi, I'm using PIL for image processing, but lately I also try numpy for the flexibility and superior speed it offers. The first thing I noticed is that for an RGB image with height=1600 and width=1900 while img=Image.open('something.tif') img.size (1900,1600) then arr=asarray(img) arr.shape (1600,1900,3) This means that the arrayimage has 1600 color channels, 1900 image pixel rows and 3 image pixel columns. Why is that? if I reshape with arr.reshape(3,1900,1600) will there be a mixup in pixel values and coordinates when compared to the initial PIL image?
2009/5/27 cp <lubensch.proletariat.inc@gmail.com>:
img=Image.open('something.tif') img.size (1900,1600)
then
arr=asarray(img) arr.shape (1600,1900,3)
This means that the arrayimage has 1600 color channels, 1900 image pixel rows and 3 image pixel columns. Why is that?
No, it means that you have 1600 rows, 1900 columns and 3 colour channels.
if I reshape with
arr.reshape(3,1900,1600)
will there be a mixup in pixel values and coordinates when compared to the initial PIL image?
You'll have to use np.rollaxis(img, 1) to get the shape you want. Regards Stéfan
arr=asarray(img) arr.shape (1600,1900,3)
No, it means that you have 1600 rows, 1900 columns and 3 colour channels.
According to scipy documentation at http://pages.physics.cornell.edu/~myers/teaching/ComputationalMethods/python... you are right. In this case I import numpy where according to http://www.scipy.org/Tentative_NumPy_Tutorial and the Printing Arrays paragraph (also in http://www.scipy.org/Numpy_Example_List#reshape reshape example) the first number is the layer, the second the rows and the last the columns. Are all the above valid or am I missing something? Thanks
cp wrote:
arr=asarray(img) arr.shape (1600,1900,3)
No, it means that you have 1600 rows, 1900 columns and 3 colour channels.
According to scipy documentation at http://pages.physics.cornell.edu/~myers/teaching/ComputationalMethods/python... you are right.
In this case I import numpy where according to http://www.scipy.org/Tentative_NumPy_Tutorial and the Printing Arrays paragraph (also in http://www.scipy.org/Numpy_Example_List#reshape reshape example) the first number is the layer, the second the rows and the last the columns.
Are all the above valid or am I missing something?
I'm not sure what part of those docs you're referring to  but they are probably both right. What you are missing is that numpy doesn't define for you what the axis mean, they just are: the zeroth axis is of length 1600 elements the first axis is of length 1900 elements the second axis is of length 3 elements what they represent is up to your application. In the case of importing from PIL, it is (height, width, rgb) (I think height and width get swapped due to how memory is laid out in PIL vs numpy) by default, numpy arrays are stored and worked with in Carray order, so that array layout has the pixels together in memory as rgb triples. Depending on what you are doing you may not want that. You can work with them any way you want: sub_region = arr[r1:r2, c1:c2, :] all_red = arr[:,:,0] but if you tend to work with all the red, or all the blue, etc, then it might be easier to rearrange it to: (rgb, height, width) If you google a bit, you'll find various notes about working with image data in numpy. HTH, Chris  Christopher Barker, Ph.D. Oceanographer Emergency Response Division NOAA/NOS/OR&R (206) 5266959 voice 7600 Sand Point Way NE (206) 5266329 fax Seattle, WA 98115 (206) 5266317 main reception Chris.Barker@noaa.gov
On Wed, May 27, 2009 at 5:12 PM, cp <lubensch.proletariat.inc@gmail.com> wrote:
Hi, I'm using PIL for image processing, but lately I also try numpy for the flexibility and superior speed it offers. The first thing I noticed is that for an RGB image with height=1600 and width=1900 while
img=Image.open('something.tif') img.size (1900,1600)
then
arr=asarray(img) arr.shape (1600,1900,3)
This means that the arrayimage has 1600 color channels, 1900 image pixel rows and 3 image pixel columns. Why is that? if I reshape with
arr.reshape(3,1900,1600)
You should look into transpose if you prefer the the colors to be on the first axis instead of the last one  that's what I like to do. Regards, Sebastian Haase
participants (4)

Christopher Barker

cp

Sebastian Haase

Stéfan van der Walt