-2
import cv2
import numpy as np
import glob

path_1='C:/Users/Akash/Downloads/good/images/*.jpg'
path_2='C:/Users/Akash/Downloads/good/labels/*.jpg'

num_1=1
num_2=1

for  file1 in glob.glob(path_1):
     for  file2 in glob.glob(path_2):
          if(file1==file2):
            img1 = cv2.imread(file1)
            img2 = cv2.imread(file2)
            dest_and = cv2.bitwise_and(img2, img1, mask = None)
            cv2.imwrite('C:/Users/Akash/Downloads/single_folder/output_images/image_'+str(num_1)+'.jpg', dest_and) 
            num_1 +=1
      num_2 +=1

I want to add 50 images from one folder to its corresponding 50 images from another folder with same filename in Python.

How can I work around the problem?

Akash Chakraborty
  • 57
  • 1
  • 2
  • 13
  • the indentation of that code is inconsistent. in python, indentation is syntax, not optional. -- where did you "find" that code? why is it "not doing anything at all"? how did you debug it? please take the [tour], review [ask], and present a [mre] -- you don't seem to have a problem using OpenCV, so this isn't an OpenCV problem. looks to me more like plain python programming, listing files, iterating... – Christoph Rackwitz Oct 05 '22 at 08:31

1 Answers1

0
 import cv2
 import glob

 image_list_1 = []
 image_list_2 = []

 path1='path of the folder where the images are'
 path2='path of the folder where the images are'

 num_1 = 1

 for file1 in glob.glob(path1 + '/*.jpg'): 
     im1=cv2.imread(file1)
     image_list_1.append(im1)

 for file2 in glob.glob(path2 + '/*.jpg'): 
     im2=cv2.imread(file2)
     image_list_2.append(im2)    

 for i in range(0,len(image_list_1)):
     im1 = image_list_1[i]
     im2 = image_list_2[i]
     dest_and = cv2.bitwise_and(im2, im1, mask = None)

     cv2.imwrite('path where the output images will be saved/image_'+str(num_1)+'.jpg', dest_and)
     num_1 += 1

This will do the work.

Akash Chakraborty
  • 57
  • 1
  • 2
  • 13