pythonnumpy图像数组:获取水平连接的图像列表,并将它们排列在网格中

pythonnumpy图像数组:获取水平连接的图像列表,并将它们排列在网格中,python,arrays,image,numpy,3d,Python,Arrays,Image,Numpy,3d,我试图从一个源文件夹中获取一个图像列表,并将它们放在一个2x2的网格中,每个图像下面都有一些文本 到目前为止,我的代码可以扫描文件夹,在每个图像下添加适当的文本,添加边框,并将图像显示在一行中,为了简洁起见,我将省略这些。我把它们都画成一条线,因为后来我想把它扩展到更大的网格尺寸,所以我的理论是把所有的图像都画成一条大线,然后再排序成一个网格: 这行图像是一个numpy数组,按如下方式连接在一起: images_line = np.hstack( (np.asarray(i) for i in

我试图从一个源文件夹中获取一个图像列表,并将它们放在一个2x2的网格中,每个图像下面都有一些文本

到目前为止,我的代码可以扫描文件夹,在每个图像下添加适当的文本,添加边框,并将图像显示在一行中,为了简洁起见,我将省略这些。我把它们都画成一条线,因为后来我想把它扩展到更大的网格尺寸,所以我的理论是把所有的图像都画成一条大线,然后再排序成一个网格:

这行图像是一个numpy数组,按如下方式连接在一起:

images_line = np.hstack( (np.asarray(i) for i in list_images_with_text ) )

print(images.ndim)  > 3
print(images.size)  > 1326000
print(images.shape) > (325, 1360, 3)
我尝试过numpy的images.reforme()将数组转换为2x2的图像网格,即:

阿西莫夫厄运

敦刻尔克金属公司

single_width = horiz_img_size;
single_height = vert_img_size + info_box_height;
images = images.reshape(2*single_height,2*single_width,3)
但这并不完全正确:

我觉得我很接近,但我不能完全了解3d阵列。我想将其扩展到更多网格大小,如4x4、5x5、5x4等。如果可能,用户可输入

这里是一个最小的可复制的例子,如果你下载原始图像

import numpy as np
import glob
import PIL
from PIL import Image

single_width = 340
single_height = 325

im=Image.open('TDYZ0.jpg')
images = np.asarray(im)

images = images.reshape(2*single_height,2*single_width,3) # not quite right

images = PIL.Image.fromarray(images)
images.save('outImage.png')    

感谢大家提供MxN图像拼接,也许您可以试试(我还没有测试):

因此,对于显示输出的2x2,它将是:

images = images.reshape(2, 2, single_height, single_width, 3)

我解决了这个问题,尽管是以一种令人厌恶的方式。生成一个字符串以形成np.block()命令的正确继承权,然后执行。基于Daniel F的评论:

M = 2
N = 2
string = "images = np.block([["
maxpos = numfiles-1;
pos = 0;
try:
    for j in range(0, N):
        for i in range(0, M):
            if pos > maxpos:
                string+= f"[np.asarray(null_image)],"
            else:
                string += f"[list_images_with_text[{pos}]]"
                pos=pos+1
                if i < M-1:
                    string+=f","
        if j < N-1:
            string+=f"],["
    string+="]])"
except:
   print("oops")
exec(string);
M=2
N=2
string=“images=np.block([]
maxpos=numfiles-1;
pos=0;
尝试:
对于范围(0,N)内的j:
对于范围(0,M)内的i:
如果pos>maxpos:
字符串+=f“[np.asarray(null_图像)],”
其他:
string+=f“[list_images_with_text[{pos}]]”
位置=位置+1
如果i
如果你想让它可扩展,你可能想让你的图像列表保持可扩展性,但我不确定如何使它可扩展/循环
images=np.block([[[list\u images\u with\u text[0]),[list_images_with_text[1]]、[list_images_with_text[2]]、[list_images_with_text[3]]]
感谢您的回复,我在这里得到了一个5d数组(2,2325340,3),PIL.Image.fromarray(images)不知道如何处理它。我不确定如何处理它here@WillHaward--我现在意识到对
PIL.Image.fromarray()的理解
在这里很重要——我根本没有。例如,一个相关的问题是:
fromarray()
是否只需要一维数组的
hxw
图像,或者它是否也接受
mxn
数组的
hxw
图像?祝你好运。
M = 2
N = 2
string = "images = np.block([["
maxpos = numfiles-1;
pos = 0;
try:
    for j in range(0, N):
        for i in range(0, M):
            if pos > maxpos:
                string+= f"[np.asarray(null_image)],"
            else:
                string += f"[list_images_with_text[{pos}]]"
                pos=pos+1
                if i < M-1:
                    string+=f","
        if j < N-1:
            string+=f"],["
    string+="]])"
except:
   print("oops")
exec(string);