shiva
shiva

Reputation: 1189

Merging images from different folders and storing to a different folder

I have three different folders m1, m2, and m3. The 'm1' folder contains images of the format image(i)_m1.png (where i =1 to N), 'm2' folder contains images of the format image(i)_m2.png, and 'm3' folder contains images of the format image(i)_m3.png. I want to merge these images using cv2.merge like this:(cv2.merge((image1_m1, image1_m2, image1_m3)) and it continues for N times and get stored in a different folder than contains 'N' merged images of the format image(i)_merged.png.

import pandas as pd
import cv2
import numpy as np
import glob
import os

filenames1 = glob.glob("data_folder/m1/*.png")
filenames1.sort()
filenames2 = glob.glob("data_folder/m2/*.png")
filenames2.sort()
filenames3 = glob.glob("data_folder/m3/*.png")
filenames3.sort()


for f1 in filenames1:
    for f2 in filenames2:
        for f3 in filenames3:
            img1 = cv2.imread(f1)
            img2 = cv2.imread(f2)
            img3 = cv2.imread(f3)
            img_m1 = cv2.cvtColor(img1, cv2.COLOR_BGR2GRAY)
            img_m2 = cv2.cvtColor(img2, cv2.COLOR_BGR2GRAY)
            img_m3 = cv2.cvtColor(img3, cv2.COLOR_BGR2GRAY)
            img_rgb = cv2.merge((img_m1, img_m2, img_m3))
            cv2.imwrite("data_folder/merge/img_name.png", img_rgb)

Upvotes: 0

Views: 618

Answers (1)

Sdhir
Sdhir

Reputation: 151

Your question is not complete. I assume you have a problem with for loop. You might replace the nested for loops with this:

for f1,f2,f3 in zip(filenames1,filenames2,filenames3):

Upvotes: 1

Related Questions