[Numpy-discussion] Size/Shape

Robert Kern robert.kern at gmail.com
Wed Jul 17 13:41:37 EDT 2013


On Wed, Jul 17, 2013 at 6:21 PM, Brady McCary <brady.mccary at gmail.com>
wrote:
>
> NumPy Folks,
>
> Would someone please discuss or point me to a discussion about the
> discrepancy in size vs shape in the following MWE? In this example I
> have used a grayscale PNG version of the ImageMagick logo, but any
> image which is not square will do.
>
> $ python
> Python 2.7.4 (default, Apr 19 2013, 18:28:01)
> [GCC 4.7.3] on linux2
> Type "help", "copyright", "credits" or "license" for more information.
> >>> import Image
> >>> import matplotlib.pyplot as plt
> >>>
> >>> s = 'logo.png'
> >>>
> >>> im = Image.open(s)
> >>> ar = plt.imread(s)
> >>>
> >>> im.size
> (640, 480)
> >>>
> >>> ar.shape
> (480, 640)
> >>>
>
> The extents/shape of the NumPy array (as loaded by matplotlib, but
> this convention seems uniform through NumPy) are transposed from what
> seems to be the usual convention. Why was this choice made?

It matches Python sequence semantics better. ar[i] will index along the
first axis to return an array of one less dimension, which itself can be
indexed (ar[i])[j]. Try using a list of lists to see what we are trying to
be consistent with. To extend this to multidimensional indexing, we want
ar[i,j] to give the same thing as ar[i][j]. The .shape attribute needs to
be given in the same order that indexing happens.

Note that what you call the "usual convention" isn't all that standard for
general multidimensional arrays. It's just one of two fairly arbitrary
choices, usually derived from the default memory layout at a very low
level. Fortran picked one convention, C picked another; numpy and Python
are built with C so we use its default conventions. Now, you are right that
image dimensions are usually quoted as (width, height), but numpy arrays
represent a much broader range of objects than images.

--
Robert Kern
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/numpy-discussion/attachments/20130717/8c8673c4/attachment.html>


More information about the NumPy-Discussion mailing list