[Numpy-discussion] Question about slicing

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

Pauli Virtanen <pav <at> iki.fi> writes:

> > img = array(img)[::-1]
> Note that here a copy is made. You can use `asarray` instead of `array` 
> if you want to avoid making a copy.

Thanks, that's good info!

> > 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?
> Sounds impossible, and I don't see this:
> In [1]: import Image
> In [2]: img = Image.open('foo.png')
> In [3]: aimg = array(img)
> In [4]: imshow(aimg)
> Out[4]: <matplotlib.image.AxesImage object at 0x9a6ecec>
> In [5]: imshow(aimg[10:320,5:150])
> Out[5]: <matplotlib.image.AxesImage object at 0x9f1db2c>
> The image is here right-side up, both in full and the slice (since imshow 
> flips it). Also,
> In [6]: aimg = array(img)[::-1]
> In [7]: imshow(aimg[10:320,5:150])
> Out[7]: <matplotlib.image.AxesImage object at 0xa007eac>
> Now, the image is upside down, both in full and in the slice.
> I think you should re-check that you are doing what you think you are 
> doing. Preparing a self-contained code example could help here, at least 
> this would make pinpointing where the error is more easy.

You're right. I was using imshow to see img (the IPL iamge, not the numpy
array), and that comes upside down, at least here. That made me think the numpy
array was upside down too when in fact it wasn't, so my 'fix' actually was
flipping it.
I'll further investigate as why the IPL image appears upside down, but my
questions about slicing are answered now. Sorry for mixing things up, and thanks
for helping out.


More information about the Numpy-discussion mailing list