【发布时间】:2017-09-02 20:48:04
【问题描述】:
我正在尝试将图像转换为 numpy 数组,当我这样做时,它给了我标题中提到的错误。 回溯错误来自以下行:
nx,ny = np.shape(matrix)
我的其余代码如下。我能有一些建议来解决这个问题吗?
#change the quoted part to change directory and
#file type
filelist = glob.glob('Desktop/*.png')
#set Matrix as the numpy array.
#change the second half were np
#is used to make the program
#use a different set of data
matrix = np.array([np.array(Image.open(fname)) for fname in filelist])
#numpy array
nx,ny = np.shape(matrix)
CXY = np.zeros([ny, nx])
for i in range(ny):
for j in range(nx):
CXY[i,j] = np.max(matrix[j,i,:])
#Binary data
np.save('/home/l/Desktop/maximums.npy', CXY)
#Human readable data
np.savetxt('/home/l/Desktop/maximums.txt', CXY)
【问题讨论】:
-
一个建议是包含您的完整回溯,以便我们知道您在哪里得到错误。
-
nx,ny = np.shape(matrix)如果矩阵只有一维,这将是您的错误来源。 -
能否包含错误回溯?
-
@DanielF 是正确的。我猜您正在使用
np.array([np.array(Image.open(fname)) for fname in filelist])创建一个维度为1 的数组,因为列表理解将生成Image对象的列表,这是不正确的。了解什么是Image以及为什么使用列表解析来加载数据需要更多详细信息。 -
这称为解包。当你有一个可迭代的(列表、元组或任何支持索引的东西)时,像 x, y = point 这样的语法会获取 point[0]、point[1] 元素并将它们分别分配给 x、y。但是,如果没有 point[0] 或 point[1] 则会抛出异常