Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Python and Numpy to blend 2 images into 1

I need to take 2 numpy.ndarrays as an arguments and iterate through each of them pixel by pixel, adding the 2 values and dividing by 2.

Essentially creating a blended image of the two and returning it as a numpy.ndarray

This is what i've come up with, but could really use some advice.

    def blendImages(image1, image2):            
        it1 = np.nditer(image1)
        it2 = np.nditer(image2)            
        for (x) in it1:
            for (y) in it2:
                newImage = (x + y) / 2
        return newImage
like image 417
yodish Avatar asked Dec 11 '22 21:12

yodish


2 Answers

You can use OpenCV function addWeighted like:

 cv2.addWeighted(img1,0.5,img2,0.5,0)`
like image 95
Miki Avatar answered Dec 13 '22 09:12

Miki


As long as the arrays are the same size:

newImage = 0.5 * image1 + 0.5 * image2
like image 40
ebo Avatar answered Dec 13 '22 11:12

ebo