NUMPY 中的图像堆叠:"FutureWarning: arrays to stack must be passed as a "sequence"类型,如列表或元组"

Images stacking in NUMPY: "FutureWarning: arrays to stack must be passed as a "sequence" type such as list or tuple"

我想把几张小图叠成一张大图。此代码有效,但给出 mi 警告:

FutureWarning: arrays to stack must be passed as a "sequence" type such as list or tuple. Support for non-sequence iterables such as generators is deprecated as of NumPy 1.16 and will raise an error in the future.

代码:

import PIL
from PIL import Image
import numpy as np

# my images are 2000x2000 resolution, but you can place here any photos with the same width and height
list_im = ['1.jpeg', '2.jpeg', '3.jpeg', '4.jpeg']

# reading images to columns:
imgsv = [PIL.Image.open(i) for i in reversed(list_im[::2])]
imgsv2 = [PIL.Image.open(i) for i in reversed(list_im[1::2])]

# vertical stacking:
imgs_comb1 = np.vstack((np.asarray(i) for i in imgsv))
imgs_comb1 = PIL.Image.fromarray(imgs_comb1)
imgs_comb1.save('V1.jpg')

imgs_comb2 = np.vstack((np.asarray(i) for i in imgsv2))
imgs_comb2 = PIL.Image.fromarray(imgs_comb2)
imgs_comb2.save('V2.jpg')

# horizontal stacking:
imgsv3 = [imgs_comb1, imgs_comb2]
imgs_comb3 = np.hstack((np.asarray(i) for i in imgsv3))
imgs_comb3 = PIL.Image.fromarray(imgs_comb3)
imgs_comb3.save('V1.jpg')

P.S。将来我需要堆叠超过 4 张图像(即 25 张)

如果能解决这个问题,我将不胜感激! :D

这是一个有趣的问题,我没有弄清楚为什么会这样。但是下面的代码在没有警告的情况下对我有用,而且它实际上有点简单。

from PIL import Image
import numpy as np

list_im = ['1.jpeg', '2.jpeg', '3.jpeg', '4.jpeg']

# reading images to columns:
imgsv = [Image.open(i) for i in reversed(list_im[::2])]
imgsv2 = [Image.open(i) for i in reversed(list_im[1::2])]

# vertical stacking:
imgs_comb1 = np.vstack(imgsv)
imgs_comb1 = Image.fromarray(imgs_comb1)
imgs_comb1.save('V1.jpg')

imgs_comb2 = np.vstack(imgsv2)
imgs_comb2 = Image.fromarray(imgs_comb2)
imgs_comb2.save('V2.jpg')

# horizontal stacking:
imgsv3 = [imgs_comb1, imgs_comb2]
imgs_comb3 = np.hstack(imgsv3)
imgs_comb3 = Image.fromarray(imgs_comb3)
imgs_comb3.save('V3.jpg')