How to add watermark on images using OpenCV in Python

Arnab Last Updated : 22 Oct, 2024
5 min read

This article was published as a part of the Data Science Blogathon

Introduction:

Watermarks are an important part of businesses and online content credit marking. It may be present in the form of a logo, signature, or stamp that is distinctive and unique to the creator. It is a very important tool when it comes to providing ownership or credits to the creator of objects in the digital world. Most professionals use watermarks to prevent their content from being stolen or replicated without their consent. Making these watermarks is now easier than ever using OpenCV.

Some of the advantages of using OpenCV to create your watermarks are as follows:

  • Ease of use– OpenCV can be used by beginners to experts to protect their intellectual property or digital creations. With a few lines of freely available code, you can make sure that no one can gain access to your photos, videos, or other digitally available content.
  • Versatile usage– OpenCV is used to create watermarks for photos ad videos using pixel manipulation via Python codes. You can also embed your watermark deep into your content so that it doesn’t disrupt your content’s visibility. This way, your work can become easily recognizable using your private watermark.

 

What is OpenCV?

It is a type of machine learning library that is Open Source and is meant for Computer vision processing functionalities and hence ti is known as OpenCV. It was built to provide a basic understanding and common infrastructure for all Computer Vision software to accelerate the growth of commercial usage. It has a BSD License and hence this makes OpenCV very easy for various businesses to modify and use the code.

It contains over 2500 different algorithms which range from normal to the latest machine learning algorithms and they all can be used in a daily project for personal use. All the algorithms can be utilized to recognize and detect faces, classify human actions, identify objects in various video feeds. It can also be used to track the camera movements, extract 3D models of any object, track moving objects, make 3D cloud points from a stereo camera. It can combine individual images to produce a higher resolution image of a whole scene, removing red eyes, understanding the background scenery, and use an augmented library. OpenCV has over 47 thousand people in their community and over 18 million downloads. As more people are using machine learning to solve their problems, the use of OpenCV is also increasing and people are creating multiple projects like gesture sensing bots or using a phone only by gestures, and many more. You can visit their website at https://opencv.org

Using OpenCV:

OpenCV

Source: https://unsplash.com/photos/0E_vhMVqL9g

OpenCV is a handy Python library that allows us to do a number of critical operations. One of the functions of this library includes the creation of watermarks. We will cover as much possible in this article but the main things which we will go through are as follows :

  • Create watermarks using text
  • Create watermarks using an image
  • Define the transparent function
  • Import the PIL function
  • Adjust the position of the text to suit your content
  • Define the function to add an image

Follow the code tagged below to begin implementing your watermark on the content you create.

Creating a watermark using the image given below :

Watermark

Source: https://unsplash.com/photos/A-NVHPka9Rk

Watermark : 

arnab mondal

Step-1: To import and load the required libraries.

import cv2
img = cv2.imread('diego-jimenez-A-NVHPka9Rk-unsplash.JPG')
watermark = cv2.imread("Watermark.JPG")

Step-2: To Scale the images 

You can scale the images and hence downsize an image for a particular resolution or keep the image to its original resolution when required.

percent_of_scaling = 20
new_width = int(img.shape[1] * percent_of_scaling/100)
new_height = int(img.shape[0] * percent_of_scaling/100)
new_dim = (new_width, new_height)
resized_img = cv2.resize(img, new_dim, interpolation=cv2.INTER_AREA)

wm_scale = 40
wm_width = int(watermark.shape[1] * wm_scale/100)
wm_height = int(watermark.shape[0] * wm_scale/100)
wm_dim = (wm_width, wm_height)

Step-3: Writing the code to create a watermark

The code snippet shown below can be used to create the watermark using OpenCV

resized_wm = cv2.resize(watermark, wm_dim, interpolation=cv2.INTER_AREA)

Step-4: The driver code function to display output

To display the image we can use the following snippet of code which will display the image in a particular output window which is generally the output screen or monitor.

h_img, w_img, _ = resized_img.shape
center_y = int(h_img/2)
center_x = int(w_img/2)
h_wm, w_wm, _ = resized_wm.shape
top_y = center_y - int(h_wm/2)
left_x = center_x - int(w_wm/2)
bottom_y = top_y + h_wm
right_x = left_x + w_wm

roi = resized_img[top_y:bottom_y, left_x:right_x]
result = cv2.addWeighted(roi, 1, resized_wm, 0.3, 0)
resized_img[top_y:bottom_y, left_x:right_x] = result

filename = 'Watermakred_Image.jpg'
cv2.imwrite(filename, resized_img)
cv2.imshow("Resized Input Image", resized_img)
cv2.waitKey(0)
cv2.destroyAllWindows()

Compiling the full code into one

To use text as a watermark on the image, we can use the code tagged below. Now we can give our image as an input and use the desired watermark and add it to any image. In this case, I have chosen a free image from the website and added my name as a watermark to it.
Python Code:

import cv2

img = cv2.imread('diego-jimenez-A-NVHPka9Rk-unsplash.jpg')

watermark = cv2.imread("Watermark.jpg")

percent_of_scaling = 20

new_width = int(img.shape[1] * percent_of_scaling / 100)

new_height = int(img.shape[0] * percent_of_scaling / 100)

new_dim = (new_width, new_height)

resized_img = cv2.resize(img, new_dim, interpolation=cv2.INTER_AREA)

wm_scale = 40

wm_width = int(watermark.shape[1] * wm_scale / 100)

wm_height = int(watermark.shape[0] * wm_scale / 100)

wm_dim = (wm_width, wm_height)

resized_wm = cv2.resize(watermark, wm_dim, interpolation=cv2.INTER_AREA)

h_img, w_img, _ = resized_img.shape

center_y = int(h_img / 2)

center_x = int(w_img / 2)

h_wm, w_wm, _ = resized_wm.shape

top_y = center_y - int(h_wm / 2)

left_x = center_x - int(w_wm / 2)

bottom_y = top_y + h_wm

right_x = left_x + w_wm

roi = resized_img[top_y:bottom_y, left_x:right_x]

result = cv2.addWeighted(roi, 1, resized_wm, 0.3, 0)

resized_img[top_y:bottom_y, left_x:right_x] = result

filename = 'Watermakred_Image.jpg'

cv2.imwrite(filename, resized_img)

cv2.imshow("Resized Input Image", resized_img)

cv2.waitKey(0)

cv2.destroyAllWindows()

# click on show files to view the output image

Conculsion

By using the above code you can add any watermark to an image and also add a custom image to your image using OpenCV and Python together. I hope this code will be beneficial to you in any way and if you like it, feel free to reach out to me in case of any issues.

Thank you for staying here till the end and get vaccinated everyone

About the Author :

Arnab Mondal

Data Engineer | Python, C/C++, AWS Developer | Freelance Tech Writer

Link to my other articles

The media shown in this article are not owned by Analytics Vidhya and are used at the Author’s discretion.

Responses From Readers

Clear

Maarten Kuijpers
Maarten Kuijpers

Please add a 'result' image of what it's supposed to look like... Then at least I'll know that your advice works before going through all those steps....

Congratulations, You Did It!
Well Done on Completing Your Learning Journey. Stay curious and keep exploring!

We use cookies essential for this site to function well. Please click to help us improve its usefulness with additional cookies. Learn about our use of cookies in our Privacy Policy & Cookies Policy.

Show details