Search code examples
pythonopencvnumpy

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

Solution

  • You can use OpenCV function addWeighted like:

     cv2.addWeighted(img1,0.5,img2,0.5,0)`