[Numpy-discussion] Question about slicing

Pauli Virtanen pav@iki...
Sat May 16 04:29:23 CDT 2009

Sat, 16 May 2009 08:42:50 +0000, jorgesmbox-ml wrote:
> 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]

Note that here a copy is made. You can use `asarray` instead of `array` 
if you want to avoid making a copy.

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

Pauli Virtanen

More information about the Numpy-discussion mailing list