Watermarking images with OpenCV and Python
In this article, we are going to see how to make watermarking images using OpenCV in Python.
Watermark is intentionally left Text/Logo onto the image. Watermarks are generally used by artists to protect the copyright of the image. Using watermarks we can ensure that the owner of the image is the person who imprinted the watermark on the image.
Let’s understand this with the help of an example, what do we mean by a watermark on an image:
Step 1: Import the OpenCV and read the logo and the image on which you want to apply a watermark.
Step 2: Calculate the height and width of both the images and save them to other variables. We need to calculate the width and height because we are going to place our watermark somewhere onto the image and for that, we just have to know the proper width and height of the both logo and the image.
Here, we have used the shape function from OpenCV, which returns the tuple of height and width of the image.
Step 3: Now, we are going to calculate the coordinates of the center of the image, because we are going to place our watermark at the center of the image.
Step 4: To add a watermark to an image we will use the addWeighted function from OpenCV. Firstly we will be providing the destination where we want to place the watermark, then we will pass that destination to the addWeighted function with the image and logo.
Syntax: cv2.addWeighted(source1, alpha, source2, beta, gamma)
In our case, source 1 will be the image where we want to place our logo and alpha will be the opacity of the logo and source2 will be the logo itself and we will set beta accordingly the alpha that is opacity and gamma will be 0.
Step 5: After that, we are simply displaying the result and saving the output. To display the output we have used imshow function and to write/save the image, we are using imwrite function in both the functions firstly we have to provide the file name as a parameter and then the file itself. cv2.waitKey(0) is used to wait until the user press Esc Key, after that the cv2.destroyAllWindows function will close the window.
Below is the full implementation: