【问题标题】:How to add multiple images from different folders in python?如何在python中添加来自不同文件夹的多个图像?
【发布时间】:2022-10-17 16:32:52
【问题描述】:
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

我想在 Python 中将一个文件夹中的 50 张图像添加到另一个文件夹中具有相同文件名的相应 50 张图像中。

我该如何解决这个问题?

【问题讨论】:

  • 该代码的缩进不一致。在 python 中,缩进是语法,不是可选的。 -- 你在哪里“找到”那个代码?为什么它“什么都不做”?你是怎么调试的?请接受tour,查看How to Ask,并提交minimal reproducible example——您使用OpenCV 似乎没有问题,所以这不是OpenCV 问题。在我看来更像是普通的 Python 编程、列出文件、迭代......

标签: python


【解决方案1】:
 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

这将完成工作。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-06-25
    • 2016-04-06
    • 2021-10-13
    • 1970-01-01
    • 1970-01-01
    • 2020-11-30
    相关资源
    最近更新 更多