简体   繁体   中英

How to overlay multiple images onto certain original images using python

I am trying to create a program in which I should be able to overlay one image over another base image using python and opencv and store the output image in an other folder. I am using opencv to achieve this however the code I have written is not giving the desired result.

import cv2
from os import listdir
from os.path import isfile, join
import numpy as np

path_wf = 'wf_flare'
path_fr = 'captured_flare'

files_wf = [ f for f in listdir(path_wf) if isfile(join(path_wf,f))]
files_fr = [ fl for fl in listdir(path_fr) if isfile(join(path_fr,fl))]

img_wf = np.empty(len(files_wf), dtype = object)
img_fr = np.empty(len(files_fr), dtype = object)
img = np.empty(len(files_wf), dtype = object)
k = 0
for n in range(0, len(files_wf)):
    img_wf[n] = cv2.imread(join(path_wf, files_wf[n]))
    img_fr[k] = cv2.imread(join(path_fr, files_fr[k]))
    print("Done Reading"+str(n))
    img_wf[n] = cv2.resize(img_wf[n], (1024,1024),interpolation = cv2.INTER_AREA)

    

    img[n] = 0.4*img_fr[k] + img_wf[n]

    fn = listdir(path_wf)
    name = 'C:\Flare\flare_img'+str(fn[n])
    print('Creating...'+ name + str(n+10991))
    cv2.imwrite(name,img[n])
    k += 1
    if(k%255 == 0):
        k = 0
    else:
        continue

the folder organization is pasted below: 在此处输入图像描述

在此处输入图像描述

在此处输入图像描述

I want the output images to come here:

在此处输入图像描述

I believe your problem is here:

name = 'C:\Flare\flare_img'+str(fn[n])

Do not just concatenate strings when you create filesystem paths. Sooner or later you end up misplacing a path separator. In this case, it is missing because fn[n] does not start with one.

Use os.path.join or the modern pathlib.Path as previously suggested.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM