Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
650 views
in Technique[技术] by (71.8m points)

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

I want to stack several small images into one big. This code works, but gives mi warning:

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.

Code:

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. In the future I will need to stack more than 4 images (i.e. 25)

I will be very grateful for some solution on this problem! :D

question from:https://stackoverflow.com/questions/65894101/images-stacking-in-numpy-futurewarning-arrays-to-stack-must-be-passed-as-a-s

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

This is an intriguing issue, I did not figure out why it happened like this. But the following code worked for me without warning, and it is actually a bit simpler.

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')

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...