Python 在numpy数组上重复函数

Python 在numpy数组上重复函数,python,numpy,tiff,Python,Numpy,Tiff,我正在尝试使两个.tif图像的图像形状匹配 我的方法是在较小图像的底部添加空白切片,直到它们的Z轴堆栈数匹配为止(假设两个图像的X和Y都具有相同的形状)。我首先将图像转换为numpy数组,然后使用np.concatenate在数组末尾添加一个带零的数组 我的代码如下所示: x = 0 difference = model.shape[1] - image.shape[1] # This line takes the difference between the larg

我正在尝试使两个.tif图像的图像形状匹配

我的方法是在较小图像的底部添加空白切片,直到它们的Z轴堆栈数匹配为止(假设两个图像的X和Y都具有相同的形状)。我首先将图像转换为numpy数组,然后使用np.concatenate在数组末尾添加一个带零的数组

我的代码如下所示:

    x = 0
    difference = model.shape[1] - image.shape[1]

    # This line takes the difference between the larger image's Z stack 
    # slice number with the smaller image and get the difference between
    # their z stack slice number.

    while x <= difference: 
        new_np_array = np.concatenate((the_image_np_array, zeros_np_array), axis=0)
        x += 1
x=0
差异=模型.形状[1]-图像.形状[1]
#这条线取较大图像的Z堆栈之间的差值
#切片数与较小的图像之间的差异
#它们的z堆栈切片数。

如果我正确理解了这个问题,则必须将串联结果分配给数组本身

  while x <= difference: 
        the_image_np_array = np.concatenate((the_image_np_array, zeros_np_array), axis=0)
        x += 1

虽然x我不确定我是否完全理解您试图实现的目标,但您是否考虑过将图像转换为PIL图像并使用Image.resize功能


附加到列表中,并在最后连接一个列表更有效。而且更容易得到正确的答案

alist = []
while x <= difference: 
    alist.append(zeros_np_array)
    x += 1
arr = np.array(alist)
# arr = np.vstack(alist) # alternative
alist=[]

X时,我会考虑把相关内容从链接添加到帖子的正文中,或者考虑把它作为评论而不是回答。