Overlay multiple images in python

Viewed 3587

My problem is that I have bunch of jpgs and I would like to overlay all of them to see a pattern.

I checked out this answer(Overlay two same sized images in Python) but it only shows how two images can be overlayed.

Here are the piece of code which shows I'd like to do.

for file in os.listdir(SAVE_DIR):
    img1 = cv2.imread(file)
    img2 = cv2.imread('next file name') #provide previous output file here (dst)

    dst = cv2.addWeighted(img1,0.5,img2,0.5,0)

    cv2.imshow('dst',dst)
    cv2.waitKey(0)
    cv2.destroyAllWindows()
1 Answers

One approach is to store all your images as a list, and then iterate through each overlapping pair of images and callcv2.addWeighted() on each element in your list, passing in the last aggregate image in as img1 to your subsequent call to cv2.addWeighted().

So for example, say you have 4 images, with names [img1, img2, img3, img4].

You could do

jpeg_list = os.listdir(SAVE_DIR)

for i in range(len(jpeg_list)):
    aggregate_file = cv2.imread(jpeg_list[i])
    next_img = cv2.imread(jpeg_list[i+1])
    dst = cv2.addWeighted(aggregate_file, 0.5, next_img, 0.5, 0)
    cv2.imshow('dst', dst)
Related