0
votes

I'm trying to combine all images one by one from two different folders, for example, I have those two folders

-folder1

-+img1.jpg

-+img2.jpg

-+img3.jpg

...

-folder2

-+img_1.jpg

-+img_2.jpg

-+img_3.jpg

...

what I would like to do is combining img1.jpg and img_1.jpg, img2.jpg and img_2.jpg ...

I'm trying with this code to get it to work but the problem takes only the same image from the folder and combines it with all images from the other folder

def load_images_from_folder(folder,folder2):
images = []
i=0
for filename in os.listdir(folder):
    for filename2 in os.listdir(folder2):
        img1 = cv2.imread(os.path.join(folder,filename))
        img2 = cv2.imread(os.path.join(folder2,filename2))
        img2 = np.fliplr(img2)
        vis = np.concatenate((img1, img2), axis=1)

Any idea to get this code worked?

1
Try concatenation only when filename == filename2 - mibrahimy
filename and filename2 doesn't have the same name, these files are in different folders. - abdou_dev

1 Answers

1
votes

You can use zip

def load_images_from_folder(folder,folder2):
    images = []
    i=0
    for filename, filename2 in zip(os.listdir(folder), os.listdir(folder2)):
        img1 = cv2.imread(os.path.join(folder,filename))
        img2 = cv2.imread(os.path.join(folder2,filename2))
        img2 = np.fliplr(img2)
        vis = np.concatenate((img1, img2), axis=1)