如何将两个图像并排连接?

时间:2019-05-26 10:55:45

标签: python image opencv concatenation cv2

我有一个文件夹,其中包含名为1_A.jpg,2_A.jpg ....等的图像。还有另一个文件夹B,其中包含名为1_B.jpg,2_B.jpg等的图像。我想串联图像1_A和1_B,2_A 2_B,依此类推。即我想串联相同的名称对图像。但是,如果我运行以下代码,则会得到1_B和1_B的串联,依此类推。

我还希望将结果存储在名为Combined_canny的文件夹中。这是我的代码。

import os
import numpy as np
import cv2
import argparse

parser = argparse.ArgumentParser('create image pairs')
parser.add_argument('--fold_A', dest='fold_A', help='input directory for image A', type=str, default='/home/vplab/pytorch-CycleGAN-and-pix2pix/datasets/canny/')
parser.add_argument('--fold_B', dest='fold_B', help='input directory for image B', type=str, default='/home/vplab/pytorch-CycleGAN-and-pix2pix/datasets/canny/')
parser.add_argument('--fold_AB', dest='fold_AB', help='output directory', type=str, default='/home/vplab/pytorch-CycleGAN-and-pix2pix/datasets/canny/combined_canny')
parser.add_argument('--num_imgs', dest='num_imgs', help='number of images', type=int, default=5000)
parser.add_argument('--use_AB', dest='use_AB', help='if true: (0001_A, 0001_B) to (0001_AB)', action='store_true')
args = parser.parse_args()

for arg in vars(args):
    print('[%s] = ' % arg, getattr(args, arg))

splits = os.listdir(args.fold_A)

for sp in splits:
    img_fold_A = os.path.join(args.fold_A, sp)
    img_fold_B = os.path.join(args.fold_B, sp)
    img_list = os.listdir(img_fold_A)
    if args.use_AB:
        img_list = [img_path for img_path in img_list if '_A.' in img_path]

    num_imgs = min(args.num_imgs, len(img_list))
    print('split = %s, use %d/%d images' % (sp, num_imgs, len(img_list)))
    img_fold_AB = os.path.join(args.fold_AB, sp)
    if not os.path.isdir(img_fold_AB):
        os.makedirs(img_fold_AB)
    print('split = %s, number of images = %d' % (sp, num_imgs))
    for n in range(num_imgs):
        name_A = img_list[n]
        path_A = os.path.join(img_fold_A, name_A)
        if args.use_AB:
            name_B = name_A.replace('_A.', '_B.')
        else:
            name_B = name_A
        path_B = os.path.join(img_fold_B, name_B)
        if os.path.isfile(path_A) and os.path.isfile(path_B):
            name_AB = name_A
            if args.use_AB:
                name_AB = name_AB.replace('_A.', '.')  # remove _A
            path_AB = os.path.join(img_fold_AB, name_AB)
            im_A = cv2.imread(path_A, 1) # python2: cv2.CV_LOAD_IMAGE_COLOR; python3: cv2.IMREAD_COLOR
            im_B = cv2.imread(path_B, 1) # python2: cv2.CV_LOAD_IMAGE_COLOR; python3: cv2.IMREAD_COLOR
            im_AB = np.concatenate([im_A, im_B], 1)
            cv2.imwrite(path_AB, im_AB)

1 个答案:

答案 0 :(得分:2)

import os
import numpy as np
import cv2
import argparse

parser = argparse.ArgumentParser('create image pairs')
parser.add_argument('--fold_A', dest='fold_A', help='input directory for image A', type=str, default='a')
parser.add_argument('--fold_B', dest='fold_B', help='input directory for image B', type=str, default='b')
parser.add_argument('--fold_AB', dest='fold_AB', help='output directory', type=str, default='./c')
parser.add_argument('--num_imgs', dest='num_imgs', help='number of images', type=int, default=1)
parser.add_argument('--use_AB', dest='use_AB', help='if true: (0001_A, 0001_B) to (0001_AB)', action='store_true')
args = parser.parse_args()

for arg in vars(args):
    print('[%s] = ' % arg, getattr(args, arg))

img_fold_A = args.fold_A
img_fold_B = args.fold_B

splits = os.listdir(args.fold_A)
img_list = os.listdir(img_fold_A)
if args.use_AB:
    img_list = [img_path for img_path in img_list if '_A.' in img_path]

num_imgs = min(args.num_imgs, len(img_list))
splits = splits[:num_imgs] # select sub array

for sp in splits:
    img_fold_AB = os.path.join(args.fold_AB, sp)
    if not os.path.isdir(img_fold_AB):
        os.makedirs(img_fold_AB)

    # print('split = %s, number of images = %d' % (sp, num_imgs))

    name_A = sp
    path_A = os.path.join(img_fold_A, name_A)
    if args.use_AB:
        name_B = name_A.replace('_A.', '_B.')
    else:
        name_B = name_A
    path_B = os.path.join(img_fold_B, name_B)
    print('path_A', path_A, path_B, os.path.isfile(path_A), os.path.isfile(path_B))
    if os.path.isfile(path_A) and os.path.isfile(path_B):
        name_AB = name_A
        if args.use_AB:
            name_AB = name_AB.replace('_A.', '.')  # remove _A
        path_AB = os.path.join(img_fold_AB, name_AB)
        im_A = cv2.imread(path_A, 1) # python2: cv2.CV_LOAD_IMAGE_COLOR; python3: cv2.IMREAD_COLOR
        im_B = cv2.imread(path_B, 1) # python2: cv2.CV_LOAD_IMAGE_COLOR; python3: cv2.IMREAD_COLOR
        im_AB = np.concatenate([im_A, im_B], 1)
        print('path_AB', path_AB)
        cv2.imwrite(path_AB, im_AB)
    else:
        print('Else')

已更新:


`img_fold_A = os.path.join(args.fold_A, sp)`  
`img_fold_B = os.path.join(args.fold_B, sp)`  

收件人

img_fold_A = args.fold_A
img_fold_B = args.fold_B


删除for n in range(num_imgs):
   并用一个loop

更新algorython