在numpy数组上重复一个函数

时间:2018-07-17 23:19:54

标签: 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 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

但是,由于该程序基本上定义了3次相同的变量,因此这是行不通的。我的问题是,如何在同一个数组上重复X次(np.concatenate)函数?

3 个答案:

答案 0 :(得分:1)

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

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

答案 1 :(得分:0)

我不确定我是否完全理解您要完成的任务,但是您是否考虑过将图像转换为PIL图像并使用Image.resize函数?

PIL Image docs

答案 2 :(得分:0)

追加到列表,并在最后连接一个,效率更高。而且更容易正确

'x':('y':"z")