如何在M * N数组中放置m * n数组?

问题描述 投票:0回答:1

我正在尝试将30px * 30px的图像拼接到3000px * 3000px的图像中。如果有比我描述的方法更好的方法,请告诉我。

我创建了BigArr = np.zeros((3000,3000))。我有图像数组(尺寸为30px * 30px)。我想放置第一张图像,使其占据BigArr[0][0]BigArr[29][29]之间的所有空间。

是否有一种简单的方法?有没有更简单的方法可以完成我想做的整体工作?

编辑:第二张图像应占据[0][30]-> [59][29],依此类推

python image numpy
1个回答
0
投票

假设完全具有BigArr所需的图像数量,并且图像可以存储在某些列表中,则可以使用所需的网格创建嵌套列表,并使用np.block生成这样的图像:] >

np.block

示例的输出如下:

from matplotlib import pyplot as plt import numpy as np # Image sizes (width x height) img_w, img_h = (40, 30) f_img_w, f_img_h = (200, 300) # Number of rows / columns in final image r = np.int32(f_img_h / img_h) c = np.int32(f_img_w / img_w) # Generate some images (stored in N-dimensional array) imgs = np.ones((img_h, img_w, r * c), np.uint8) * 5 * (np.arange(r * c) + 1) # Assumed starting point of procedure: All images stored in list imgs_list = [imgs[:, :, i] for i in np.arange(imgs.shape[2])] # Generate nested list with desired grid imgs_nested_list = [[imgs_list[y * c + x] for x in np.arange(c)] for y in np.arange(r)] # Generate desired image big_arr = np.block(imgs_nested_list) plt.figure(1) plt.imshow(big_arr, vmin=0, vmax=255) plt.show()

希望有帮助!

Output
© www.soinside.com 2019 - 2024. All rights reserved.