Overlay n list of same size images on each other in sequence they appear in list
Viewed 3 times
I have a requirement where the python3 function will accept 3 positional arguments (list of image paths, 'outfile name', destination path)
This is the code I coded which works as expected, But I looking for a more better way of doing it.
def overlay_iamges(jpeg_list, outfile, destination):
from PIL import Image
import os
from shutil import copyfile, move
new_list = []
for each in jpeg_list:
copyfile(each, os.path.join(os.getcwd(), os.path.basename(each)))
new_list.append(os.path.basename(each))
for i in range(1, len(new_list)):
background = Image.open(new_list[i-1])
img = Image.open(new_list[i])
background.paste(img, (0, 0), img)
background.save(new_list[i],new_list[0].split('.')[-1].upper())
new_file_name = outfile+'.'+new_list[-1].split('.')[-1].lower()
os.rename(new_list[-1], new_file_name)
move(os.path.join(os.getcwd(), new_file_name), os.path.join(destination, new_file_name))
for each in new_list[:-1]:
os.remove(each)
jpeg_list = [r'C:\Users\samant\Google Drive\test/blackbackground.png', r'C:\Users\samant\Google Drive\test/bluesquare.png', r'C:\Users\samant\Google Drive\test/redcircle.png']
overlay_iamges(jpeg_list, 'outfile', r'C:\Users\samant\Google Drive\destination')
Attached below are input images :
Expected output below :