[Numpy-discussion] Question about slicing

Chris Colbert sccolbert@gmail....
Sat May 16 22:12:42 CDT 2009


the reason for all this is that the bitmap image format specifies the image
origin as the lower left corner. This is the convention used by PIL. The
origin of a numpy array is the upper right corner. Matplot lib does not
handle this discrepancy in the function pil_to_array, which is called
internally when you invoke imshow(img) on a PIL image. Recently, PIL has
implemented the array interface for PIL images. So if you call asarray(img)
on a PIL image, you will get a (height, width, 3) array (for RGB) with the
origin in the upper left corner. This is why the image appears right side up
in matplotlib doing things this way.

The matplot lib code should probably be updated to make use of the array
interface. It just reshapes the raw string data currently.

Chris

On Sat, May 16, 2009 at 6:42 PM, Jorge Scandaliaris
<jorgesmbox-ml@yahoo.es>wrote:

> Pauli Virtanen <pav <at> iki.fi> writes:
>
> <snip>
> > > 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.
>
> Jorge
>
>
>
>
>
> _______________________________________________
> Numpy-discussion mailing list
> Numpy-discussion@scipy.org
> http://mail.scipy.org/mailman/listinfo/numpy-discussion
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.scipy.org/pipermail/numpy-discussion/attachments/20090516/cecaaf51/attachment.html 


More information about the Numpy-discussion mailing list