使用np.concatenate(两个垂直和两个水平)连接4个图像

2024-06-28 20:40:05 发布

您现在位置:Python中文网/ 问答频道 /正文

我有四个图像,我想将它们连接成一个图像。我尝试使用连接函数,但有多个图像,我只需要在一批4个图像中使用它们

实际上,这些图像被命名为0.jpg, 1.jpg,2.jpg and 3.jpg 下面是包含四个图像并将它们连接为一个图像的代码。但我在一个文件夹中有大约500张图像,我想根据范围将它们分成四对,比如first4,Second4,等等

import numpy as np 
import glob,os
import cv2

directory = "./image/"

for image in sorted(glob.glob(directory + '*.jpg'),key=os.path.getmtime):   

    name = image.split('/')[-1]
    iname = name.split('.')[0]
    print(iname)                    
    img1 = cv2.imread('0.jpg') 
    img2 = cv2.imread('1.jpg')
    vis1 = np.concatenate((img1, img2), axis=1)
    img3 = cv2.imread('2.jpg')
    img4 = cv2.imread('3.jpg')
    vis2 = np.concatenate((img3, img4), axis=1)
    vis = np.concatenate((vis1, vis2), axis=0)
    cv2.imwrite(iname+'.jpg', vis1)

Tags: name图像imageimportosnpcv2directory
1条回答
网友
1楼 · 发布于 2024-06-28 20:40:05

这应该会奏效,或者至少让你开始。它选择红色作为背景色,并使1x1填充(填充)图像具有相同的颜色。然后将图像按4分组,如果不是4的倍数,则pad填充最后一组。然后在列表上迭代,每次迭代打开4个图像。它获取它们的大小,然后确定输出图像的宽度和高度。然后创建填充背景颜色的输出图像,然后将图像粘贴到背景上并保存结果

您可以选择改进布局,但这只是摆弄美学,所以您可以做到这一点

#!/usr/bin/env python3

import cv2
import os, glob
from itertools import zip_longest

def grouper(iterable, n, fillvalue=None):
    """
    Group items of list in groups of "n" padding with "fillvalue"
    """
    args = [iter(iterable)] * n
    return zip_longest(*args, fillvalue=fillvalue)

# Go to where the images are instead of managing a load of complicated paths
os.chdir('images')

# Get list of filenames sorted by mtime
filenames = sorted(glob.glob('*.jpg'),key=os.path.getmtime)

# Define a background colour onto which you will paste the images
bg = [0,0,255]   # background = red

# Make a 1x1 filler image as a PNG so that it doesn't appear in your globbed list of JPEGs
# Make it same as background colour so it doesn't show
fill = np.full((1,1,3), bg, dtype=np.uint8)
cv2.imwrite('fill.png', fill)

# Iterate over the files in groups of 4
out = 1
for f1, f2, f3, f4 in grouper(filenames, 4, 'fill.png'):
    outfile = f'montage-{out}.jpg'
    print(f'DEBUG: Merging {f1},{f2},{f3},{f4} to form {outfile}')
    out += 1

    # Load all 4 images
    i1 = cv2.imread(f1)
    i1h, i1w = i1.shape[:2]
    i2 = cv2.imread(f2)
    i2h, i2w = i2.shape[:2]
    i3 = cv2.imread(f3)
    i3h, i3w = i3.shape[:2]
    i4 = cv2.imread(f4)
    i4h, i4w = i4.shape[:2]

    # Decide width of output image
    w = max(i1w+i2w, i3w+i4w)
    # Decide height of output image
    h = max(i1h,i2h) + max(i3h,i4h)

    # Make background image of background colour onto which to paste 4 images
    res = np.full((h,w,3), bg, dtype=np.uint8)

    # There are fancier layouts, but I will just paste into the 4 corners
    res[0:i1h, 0:i1w,  :] = i1       # image 1 into top-left
    res[0:i2h, w-i2w:, :] = i2       # image 2 into top-right
    res[h-i3h:,0:i3w,  :] = i3       # image 3 into bottom-left
    res[h-i4h:,w-i4w:, :] = i4       # image 4 into bottom-right

    # Save result image
    cv2.imwrite(outfile, res)

它创建如下输出图像:

enter image description here


注意:如果使用ImageMagick,只需使用几行bash shell脚本即可完成相同的操作:

#!/bin/bash

# Build list of images
images=(*.jpg)

out=1
# Keep going till there are fewer than 4 left in list
while [ ${#images[@]} -gt 3 ] ; do
   # Montage first 4 images from list
   magick montage -geometry +0+0 -tile 2x2 -background yellow "${images[@]:0:4}" "montage-${out}.png"
   # Delete first 4 images from list
   images=(${images[@]:4})
   ((out+=1))
done

关键词:Python、OpenCV、蒙太奇、组、分组、按四、按四

相关问题 更多 >