[Numpy-discussion] Question about slicing

Jorge Scandaliaris jorgesmbox-ml@yahoo...
Sat May 16 17:05:16 CDT 2009


Emmanuelle Gouillart <emmanuelle.gouillart <at> normalesup.org> writes:

> 
> 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.
> 

Well, if by 3D you mean color images then yes, PIL is able to handle them

> 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...
> 

You're right. I should have done these tests myself. I apologize for jumping to
the list so quickly.

> 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?) ?
>

It is weird indeed. But no so much that they appear upside down (I do use
pylab.imshow() to display images), because at the end of the day it is just a
convention, and different things can use different conventions, but because of
the fact that the numpy array obtained from the PIL image is not. 

I downloaded the scipy logo:
http://docs.scipy.org/doc/_static/scipyshiny_small.png and did the following:

img = Image.open('./scipyshiny_small.png')

mpl.pylab.imshow(img)  # Comes upside down

aimg = asarray(img)

mpl.pylab.imshow(aimg) # Comes OK

I guess my problem lies with PIL rather than with numpy. I am glad to find that
slicing works as I would have expected it to work!

 
> Hope someone can help you more than I did :D

You did help, thanks!

Jorge



More information about the Numpy-discussion mailing list