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

时间:2020-02-19 03:23:35

标签: python image numpy

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

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

有没有简单的方法可以做到这一点?有没有一种更简单的方法可以完成我想做的事情?

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

1 个答案:

答案 0 :(得分:0)

假设您拥有完全填满BigArr所需的图像,并且图像可以存储在某些列表中,则可以使用所需的网格创建嵌套列表,并使用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

希望有帮助!

----------------------------------------
System information
----------------------------------------
Platform:    Windows-10-10.0.16299-SP0
Python:      3.8.1
Matplotlib:  3.2.0rc3
NumPy:       1.18.1
----------------------------------------
相关问题