Open In App

Watermarking images with OpenCV and Python

Improve
Improve
Like Article
Like
Save
Share
Report

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.jpg

Step-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



Last Updated : 03 Jan, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads