cv2 addweighted Implementation: Example with Steps

cv2 addweighted Implementation

OpenCV is the best library for image processing. Using it you can manipulate any images. To do so it has many inbuilt functions for it. In this entire tutorial, you will learn how to apply cv2 addweighted function for adding two images.

Syntax of the cv2 addwighted function

Before going to the coding demonstration let’s know the syntax for the cv2 addweighted method.

cv2.addWeighted(src1, alpha, src2, beta,y)

Here src1 is the path for the first image and scr2 is the path for the second image.  The alpha and beta are the constant or weight for the first and second image respectively. The “y”  is some constant. The above function follows the below equation.

cv2 addwighted euqation
cv2 addwighted equation

Steps to Implement cv2 addweighted method

In this entire section, you will learn how to apply cv2 addwighted method for joining two images in a single image. Just follow the steps for deep understanding.

Step 1: Import required packages

The first step is to import all the necessary libraries that require for this tutorial. Here In this example, I am using only OpenCV python packages. So let’s import it using the import statement.

import cv2

Step 2: Read the images

The second step is to read the image. Here I will read two images. One for the parent image and the other image for merging it with the parent image.

In OpenCV, you can read the image using the cv2.imread() method. Use the below lines of code to read the two images.

img1 = cv2.imread("bird.jpg",1)
img2 = cv2.imread("bird2.jpg",1)
bird image
First Image
Bird image
Second Image

Make sure the image should be of the same size. otherwise, it will produce an error. In our example, the images are of different sizes. So I am resizing the second image to the same dimension as the first image using the cv2.resize() method.’

img2_resized= cv2.resize(img2,(640,426))

Here 1 is used to read the colored image. If you will 0 then it will read the image as a gray image.

Step 3: Implement the cv2 addweighted method

After reading the two images let’s implement the cv2.addweighted() method by passing the two input images as an argument to the function. Here you have to also define the weightage for each image that is alpha and beta. In this example, I am setting the alpha value to 0.3 and the beta value to 0.7. The constant y is 0. Add the following line of code to continue.

img = cv2.addWeighted(img1,0.3,img2_resized,0.7,0)

Step 4: Show the image

Now after adding two images in one image. Let’s display it and compare it with the two images. In OpenCV, You can show the images using the cv2.imshow() method.

cv2.imshow("First Image",img1)
cv2.imshow("Second Image",img2_resized)

img = cv2.addWeighted(img1,0.3,img2_resized,0.7,0)
cv2.imshow("Added Image",img)


Added image for the cv2 addweighted method
Added image for the cv2 addweighted method


The cv2 addwighted() is very useful if you want to merge two or more images in one image. These are the steps to implement the cv2.addwighted method in python. I hope you have liked this article. If you have any queries then you can contact us for more help. You can also subscribe to us for getting updates on new tutorials directly in your inbox.

Join our list

Subscribe to our mailing list and get interesting stuff and updates to your email inbox.

Thank you for signup. A Confirmation Email has been sent to your Email Address.

Something went wrong.

Meet Sukesh ( Chief Editor ), a passionate and skilled Python programmer with a deep fascination for data science, NumPy, and Pandas. His journey in the world of coding began as a curious explorer and has evolved into a seasoned data enthusiast.
Thank you For sharing.We appreciate your support. Don't Forget to LIKE and FOLLOW our SITE to keep UPDATED with Data Science Learner