2014-03-18 66 views

回答

9

您可以使用元組解壓縮。

y, x = a.shape 
+0

謝謝。谷歌搜索它並沒有建立任何東西! :D – pceccon

+1

'.shape'是一個numpy數組的屬性,而不是一個方法。 – wflynny

+0

注意:'.shape()'(帶圓括號)最初是在問題本身中提到的,所以它看起來更像是注意力不集中,而不是邏輯錯誤。 – ffriend

4
height, width = a.shape 

但是請注意,該ndarray具有矩陣座標(i,j),這是相反的圖像座標(x,y)。那就是:

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

此外,Python的元組支持索引,以便您可以訪問不同的尺寸是這樣的:

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

ndarray.shape()將拋出一個TypeError: 'tuple' object is not callable.,因爲它不是一個功能,這是一個值。

你想要做的只是元組解壓.shape沒有()。例如:

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

+0

在你的第二段中,你可能指'.shape',而不是'.size'。 '.size'包含元素的總數。 – ffriend

+0

是的,我的錯誤。 – Ford

相關問題