简体   繁体   中英

How can I get the x and y dimensions of a ndarray - Numpy / Python

I'm wondering if I can get the x and y dimensions of a ndarray separately. I know that I can use ndarray.shape to get a tuple representing the dimensions, but how can I separate this in x and y information?

Thank you in advance.

你可以使用元组解包。

y, x = a.shape
height, width = a.shape

Note, however, that ndarray has matrix coordinates ( i,j ), which are opposite to image coordinates ( x,y ). That is:

i, j = y, x  # and not x, y

Also, Python tuples support indexing, so you can access separate dimensions like this:

dims = a.shape
height = dims[0]
width = dims[1]

ndarray.shape() will throw a TypeError: 'tuple' object is not callable. because it's not a function, it's a value.

What you want to do is just tuple unpack .shape without the () . Example:

>> import numpy
>> ndarray = numpy.ndarray((20, 21))
>> ndarray.shape
(20, 21)
>> x, y = ndarray.shape
>> x
20
>> y
21

http://docs.scipy.org/doc/numpy/reference/generated/numpy.ndarray.shape.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM