您的意思是这样吗?
import numpy as np
img_1 = np.ones((2, 2))
img_2 = np.ones((2, 2)) * 2
img_3 = np.ones((2, 2)) * 3
img = np.hstack([img_1, img_2, img_3])
new_shape = np.array([6, 12])
new_img = np.tile(img, reps=new_shape // np.array(img.shape))
如果您想要调整图像大小,也许this answer可以帮到您。
这种方法不关心您如何获取img
。重要的是,您必须确保所有初始图像的组合将添加到最终形状。
如果您有形状为50x50
的图像和最终形状为500x500
,并且您总是将它们中的2、3或4个组合在一起,则会出现以下情况:
- 500 / (2*50) = 5
- 500 / (3*50) = 3.33
- 500 / (4*50) = 2.5
所以这对您来说行不通。
您有两个选择,要么裁剪不适合的像素,要么调整结果图像的大小。
- 500 / (2*50) = 5
- 500 / (3*50) = 3.33 -> 4,将500x600的图像裁剪/缩放为500x500
- 500 / (4*50) = 2.5 -> 3,将500x600的图像裁剪/缩放为500x500
import numpy as np
img_1 = np.ones((2, 2))
img_2 = np.ones((2, 2)) * 2
img_3 = np.ones((2, 2)) * 3
img_4 = np.ones((2, 2)) * 4
img_list = np.array([img_1, img_2, img_3, img_4])
img_dx_rnd = np.random.choice(np.arange(4), np.random.randint(2, 5), replace=False)
img = np.hstack([*img_list[img_dx_rnd]])
new_shape = np.array([6, 12])
reps = np.ceil(new_shape / np.array(img.shape)).astype(int)
new_img = np.tile(img, reps=reps)
new_img = new_img[:new_shape[0], :new_shape[1]]
from skimage.transform import resize
new_img = resize(new_img, new_shape)