Watermarking images with OpenCV and Python
Last Updated :
03 Jan, 2023
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:
Image before watermark
Logo:
logo.jpgStep-by-Step implementation:
Step 1: Import the OpenCV and read the logo and the image on which you want to apply a watermark.
Python3
# watermarking image using OpenCV
# importing cv2
import cv2
# importing logo that we are going to use
logo = cv2.imread("logo.jpg")
# importing image on which we are going to
# apply watermark
img = cv2.imread("dark.png")
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.
Python3
# calculating dimensions
# height and width of the logo
h_logo, w_logo, _ = logo.shape
# height and width of the image
h_img, w_img, _ = img.shape
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.
Python3
# calculating coordinates of center
# calculating center, where we are going
# to place our watermark
center_y = int(h_img/2)
center_x = int(w_img/2)
# calculating from top, bottom, right and left
top_y = center_y - int(h_logo/2)
bottom_y = top_y + h_logo
right_x = left_x + w_logo
left_x = center_x - int(w_logo/2)
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.
Python3
# adding watermark to the image
destination = img[top_y:bottom_y, left_x:right_x]
result = cv2.addWeighted(destination,1, logo, 0.5, 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.
Python3
# displaying and saving image
img[top_y:bottom_y, left_x:right_x] = result
cv2.imwrite("watermarked.jpg", img)
cv2.imshow("Watermarked Image", img)
cv2.waitKey(0)
cv2.destroyAllWindows()
Below is the full implementation:
Python3
# watermarking image using OpenCV
# importing cv2
import cv2
# loading images
# importing logo that we are going to use
logo = cv2.imread("logo.jpg")
# importing image on which we are going to
# apply watermark
img = cv2.imread("dark.png")
# calculating dimensions
# height and width of the logo
h_logo, w_logo, _ = logo.shape
# height and width of the image
h_img, w_img, _ = img.shape
# calculating coordinates of center
# calculating center, where we are going to
# place our watermark
center_y = int(h_img/2)
center_x = int(w_img/2)
# calculating from top, bottom, right and left
top_y = center_y - int(h_logo/2)
left_x = center_x - int(w_logo/2)
bottom_y = top_y + h_logo
right_x = left_x + w_logo
# adding watermark to the image
destination = img[top_y:bottom_y, left_x:right_x]
result = cv2.addWeighted(destination, 1, logo, 0.5, 0)
# displaying and saving image
img[top_y:bottom_y, left_x:right_x] = result
cv2.imwrite("watermarked.jpg", img)
cv2.imshow("Watermarked Image", img)
cv2.waitKey(0)
cv2.destroyAllWindows()
Output:
After watermarked
Similar Reads
Reading an image in OpenCV using Python
Prerequisite: Basics of OpenCVIn this article, we'll try to open an image by using OpenCV (Open Source Computer Vision) library.  Following types of files are supported in OpenCV library:Windows bitmaps - *.bmp, *.dibJPEG files - *.jpeg, *.jpgPortable Network Graphics - *.png WebP - *.webp Sun raste
6 min read
Storing OpenCV Image in SQLite3 with Python
OpenCV is a huge open-source library for computer vision, machine learning, and image processing. OpenCV supports a wide variety of programming languages like Python, C++, Java, etc. It can process images and videos to identify objects, faces, or even the handwriting of a human. When it is integrate
3 min read
Drawing with Mouse on Images using Python-OpenCV
OpenCV is a huge open-source library for computer vision, machine learning, and image processing. OpenCV supports a wide variety of programming languages like Python, C++, Java, etc. It can process images and videos to identify objects, faces, or even the handwriting of a human. In this article, we
3 min read
Convert BGR and RGB with Python - OpenCV
Prerequisites: OpenCV OpenCV is a huge open-source library for computer vision, machine learning, and image processing. OpenCV supports a wide variety of programming languages like Python, C++, Java, etc. It can process images and videos to identify objects, faces, or even the handwriting of a human
2 min read
Crop Image with OpenCV-Python
Cropping an Image is one of the most basic image operations that we perform in our projects. In this article, we will discuss how to crop images using OpenCV in Python. Stepwise Implementation For this, we will take the image shown below. Step 1: Read the image cv2.imread() method loads an image fr
2 min read
Splitting and Merging Channels with Python-OpenCV
In this article, we will learn how to split a multi-channel image into separate channels and combine those separate channels into a multi-channel image using OpenCV in Python. To do this, we use cv2.split() and cv2.merge() functions respectively. Image Used: Splitting Channels cv2.split() is used
2 min read
Image Resizing using OpenCV | Python
Image resizing refers to the scaling of images. Scaling comes in handy in many image processing as well as machine learning applications. It helps in reducing the number of pixels from an image and that has several advantages e.g. It can reduce the time of training of a neural network as the more th
3 min read
Adding borders to the images using Python - OpenCV
Image processing is an interesting field in today's era of Artificial Intelligence and Machine Learning. We can see the applications of image processing in our day-to-day life, like whenever we apply filter over any image (selfie) or when we want to apply some effect like blurring the image, etc. I
1 min read
Animate image using OpenCV in Python
In this article, we will discuss how to animate an image using python's OpenCV module. Let's suppose we have one image. Using that single image we will animate it in such a way it will appear continuous array of the same image. This is useful in animating the background in certain games. For example
3 min read
Getting Started with Python OpenCV
Computer Vision is one of the techniques from which we can understand images and videos and can extract information from them. It is a subset of artificial intelligence that collects information from digital images or videos. Python OpenCV is the most popular computer vision library. By using it, o
15+ min read