无法将形状(1120,1472,4)的输入数组广播到形状(3000,3000)



我有一个形状为(1120,1472,4)的图像,我试图将其更改为(3000,3000,4),我写的代码是这样的。

pad_shape = (3000, 3000)
i = np.array('test.tif')
result = np.zeros(pad_shape,dtype=int)
result[:i.shape[0], :i.shape[1]] = i
print(result)

生成

ValueError: could not broadcast input array from shape (1120, 1472, 4) into shape (3000, 3000)

答案如下

import numpy as np
from numpy import dtype
pad_shape = np.array((3000, 3000, 4))
i = np.zeros((1120, 1472, 4), dtype=np.int)
result = np.zeros(pad_shape, dtype=np.int)
x, y = 10, 10
shape = i.shape
print shape
rr = result[y:y+shape[0], x:x+shape[1]]
print rr

x和y变量用于填充。由于填充而收到的错误。

最新更新