[Numpy-discussion] Question about slicing

Emmanuelle Gouillart emmanuelle.gouillart@normalesup....
Sat May 16 04:22:45 CDT 2009


Hi Jorge,

> roi = aimg[10:20,45:50,:]

are you working with 3-D images? I didn't know PIL was able to handle 3D
images.

I wasn't able to reproduce the behavior you observed with a simple
example:
In [20]: base = np.arange(25).reshape((5,5))

In [21]: base
Out[21]: 
array([[ 0,  1,  2,  3,  4],
       [ 5,  6,  7,  8,  9],
       [10, 11, 12, 13, 14],
       [15, 16, 17, 18, 19],
       [20, 21, 22, 23, 24]])

In [22]: flip = base[::-1]

In [23]: flip
Out[23]: 
array([[20, 21, 22, 23, 24],
       [15, 16, 17, 18, 19],
       [10, 11, 12, 13, 14],
       [ 5,  6,  7,  8,  9],
       [ 0,  1,  2,  3,  4]])

In [24]: flip[2:4,2:4]
Out[24]: 
array([[12, 13],
       [ 7,  8]])
which is what you expect...

I also tried the same manipulations as you do starting from a PIL image
object, but I also got what I expected (and my image was not flipped
vertically by PIL or when transformed into an array). It is quite weird
BTW that your images are flipped. How do you visualize PIL image
(Image.Image.show?)and arrays (pylab.imshow?) ?

Hope someone can help you more than I did :D

Cheers,

Emmanuelle

On Sat, May 16, 2009 at 08:42:50AM +0000, jorgesmbox-ml@yahoo.es wrote:

> Hi,

> I am just starting with numpy, pyhton and related others. I work on image processing basically. Now, my question is: what is the expected behaviour when slicing a view of an array? The following example might give some background on what I tried to do and the results obatined (which I don't understand):

> I read an image with PIL but, for whatever reason (different conventions I suppose), it comes upside down. This doesn't change when (I don't know the exact term for this) transforming the image to ndarray with 'array(img)'. I don't feel comfortable working with upside down images, so this had to be fixed. I tried to be smart and avoid copying the whole image:

> aimg = array(img)[::-1]

> and it worked!, but I am interested actually in sub-regions of this image, so the next I did was:

> roi = aimg[10:20,45:50,:]

> And to my surprise the result was like if I was slicing the original, upside down, image instead of aimg. Can someone explain me what's going on here? I searched and looked at the documentation but I couldn't find an answer. Maybe I am not looking properly. Is the only way to turn the image to perform a copy?

> Thanks,

> Jorge




> _______________________________________________
> Numpy-discussion mailing list
> Numpy-discussion@scipy.org
> http://mail.scipy.org/mailman/listinfo/numpy-discussion


More information about the Numpy-discussion mailing list