Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/360.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何将文件夹中的所有图像垂直合并/附加/堆叠/粘贴到新图像_Python_Numpy_Opencv_Python Imaging Library - Fatal编程技术网

Python 如何将文件夹中的所有图像垂直合并/附加/堆叠/粘贴到新图像

Python 如何将文件夹中的所有图像垂直合并/附加/堆叠/粘贴到新图像,python,numpy,opencv,python-imaging-library,Python,Numpy,Opencv,Python Imaging Library,我想合并一个文件夹中的所有图片,并有他们堆叠,附加水平 import os from PIL import Image allfiles = os.listdir(os.getcwd()) imlist =[filename for filename in allfiles if filename[-4:] in [".png", ".PNG"]] N = len(imlist) w, h = Image.open(imlist[0]).size tot

我想合并一个文件夹中的所有图片,并有他们堆叠,附加水平

import os
from PIL import Image
allfiles = os.listdir(os.getcwd())
imlist =[filename for filename in allfiles if filename[-4:] in [".png", ".PNG"]]
N = len(imlist)
w, h = Image.open(imlist[0]).size

total_width = w * N
max_height = h

new_im = Image.new('RGB', (total_width, max_height))

for i in range(1,N):
    img = Image.open(imlist[i])
    offset = 0
    appendedimages.paste(img, (x_offset,0))
    offset += img.size[0]

appendedimages.save('test.jpg')

看起来,使用这个,它只显示最后一个图像。有人知道为什么会这样吗

我也试过了

import cv2
import os
import numpy as np


allfiles = os.listdir(os.getcwd())
imlist =[filename for filename in allfiles if filename[-4:] in [".png", ".PNG"]]
N = len(imlist)
for i in range(1,N):
    img = cv2.imread(imlist[i])

horizontalAppendedImg = np.hstack(img)
cv2.imshow('Horizontal Appended', horizontalAppendedImg)
cv2.waitKey(0)
cv2.destroyAllWindows()
但这也不起作用


有人能帮我吗?或者有更简单的解决方案吗?

图像是numpy数组。只要它们具有相同的维度,您就可以
np.hstack
它们

imlist =[cv2.imread(filename) for filename in allfiles if filename[-4:] in [".png", ".PNG"]]
concat_img = np.hstack(imlist)

cv2.imshow('Horizontal Appended', concat_img)
cv2.waitKey(0)
cv2.destroyAllWindows()
说明:

  • 通过列表理解将所有图像读入列表:图像列表/
    np.数组
  • 使用
    np.hstack()。如果遇到问题,可以将更通用的连接工具与
    np.concatenate()
    np.stack()
    结合使用

图像是numpy阵列。只要它们具有相同的维度,您就可以
np.hstack
它们

imlist =[cv2.imread(filename) for filename in allfiles if filename[-4:] in [".png", ".PNG"]]
concat_img = np.hstack(imlist)

cv2.imshow('Horizontal Appended', concat_img)
cv2.waitKey(0)
cv2.destroyAllWindows()
说明:

  • 通过列表理解将所有图像读入列表:图像列表/
    np.数组
  • 使用
    np.hstack()。如果遇到问题,可以将更通用的连接工具与
    np.concatenate()
    np.stack()
    结合使用

这个问题结束了吗?这个问题结束了吗?