Addition and Blending of images using OpenCV in PythonReadDiscussCoursesPracticeImprove Article ImproveSave Article SaveLike Article LikeWhen we talk about images, we know its all about the matrix either binary image(0, 1), gray scale image(0-255) or RGB image(255 255 255). So additions of the image is adding the numbers of two matrices. In OpenCV, we have a command cv2.add() to add the images.Below is code for Addition of two images using OpenCV :# Python program for adding# images using OpenCV # import OpenCV fileimport cv2 # Read Image1mountain = cv2.imread('F:\mountain.jpg', 1) # Read image2dog = cv2.imread('F:\dog.jpg', 1) # Add the imagesimg = cv2.add(mountain, dog) # Show the imagecv2.imshow('image', img) # Wait for a keycv2.waitKey(0) # Distroy all the window opencv2.distroyAllWindows()But sometimes we do not want to perform simple addition in image, so in this case we have blending. This is also image addition, but different weights are given to images so that it gives a feeling of blending or transparency. Images are added as per the equation below :g(x) = (1 - a)f(x) + af1(x)By varying a from 0 -> 1, you can perform a cool transition between one image to another. Here two images are taken to blend together. First image is given a weight of 0.3 and second image is given 0.7, cv2.addWeighted() applies following equation on the image :img = a . img1 + b . img 2 + yHere y is taken as zero.Below is code for Blending of images using OpenCV :# Python program for blending of# images using OpenCV # import OpenCV fileimport cv2 # Read Image1mountain = cv2.imread('F:\mountain.jpg', 1) # Read image2dog = cv2.imread('F:\dog.jpg', 1) # Blending the images with 0.3 and 0.7img = cv2.addWeighted(mountain, 0.3, dog, 0.7, 0) # Show the imagecv2.imshow('image', img) # Wait for a keycv2.waitKey(0) # Distroy all the window opencv2.distroyAllWindows()Last Updated : 04 Jan, 2023Like Article Save Article Please Login to comment...Similar ReadsOpenCV - Alpha blending and masking of imagesArithmetic Operations on Images using OpenCV | Set-1 (Addition and Subtraction)Arithmetic Operations on Images using OpenCV | Set-2 (Bitwise Operations on Binary Images)Blending of two videos using PythonPython | Create video using multiple images using OpenCVErosion and Dilation of images using OpenCV in pythonOpenCV - Facial Landmarks and Face Detection using dlib and OpenCVPython | Denoising of colored images using opencvPython | Grayscaling of Images using OpenCVHow to Detect Shapes in Images in Python using OpenCV?Related TutorialsOpenAI Python API - Complete GuidePandas AI: The Generative AI Python LibraryPython for Kids - Fun Tutorial to Learn Python ProgrammingData Analysis TutorialFlask Tutorial LikePreviousGenerating Random id's using UUID in PythonNext Extracting email addresses using regular expressions in PythonArticle Contributed By :KKishor MishraKishor Mishra FollowVote for difficultyCurrent difficulty : MediumEasy Normal Medium Hard ExpertArticle Tags :Image-ProcessingOpenCVPython-OpenCVPythonPractice Tags :pythonReport Issue